如何将按钮添加到按钮以及可以在按钮中放置的所有内容?

时间:2017-08-31 23:17:27

标签: javascript events dom

JS事件有问题。

<button class="TabsItem active">
  <img src="images/type-icons/120.PNG" alt="">
</button>

addEventListener使用.TabItem,但当我点击<img />时,什么也没发生。无法弄清楚如何修复它,以便可以出现在按钮中的所有内容都会对事件做出反应。

通过搜索和谷歌搜索找不到答案。 感谢。

UPD: 这是js部分,它依赖于对象数组:

let buffer = document.querySelectorAll('.Tabs'),
        Tabs = [];

buffer.forEach(el => {
    Tabs.push({
        List: el,
        Switches: el.querySelectorAll('.TabsItem'),
        Contents: el.querySelectorAll('.TabsContentItem'),
    })
});
buffer = [];

Tabs.forEach(currentTab => {
        currentTab.Switches.forEach(tabSwitch => {
            tabSwitch.addEventListener('click', (e) => {
                e.stopPropagation();
                const currentButton = e.target;
                if ((currentButton == tabSwitch) && (!currentButton.classList.contains('active'))) {
                    currentTab.Switches.forEach(currentTabSwitchClear => { currentTabSwitchClear.classList.remove('active'); });
                    currentTab.Contents.forEach(currentTabContentClear => { currentTabContentClear.classList.remove('active'); });
                    currentButton.classList.add('active');
                    currentTab.Contents[Array.from(currentTab.Switches).indexOf(currentButton)].classList.add('active');
                }
            });
        });
    });

2 个答案:

答案 0 :(得分:2)

不要设置currentButton = e.target,因为e.target将等于Image元素而不是按钮。 currentButton将等于tabSwitch。

试试这个。

&#13;
&#13;
var button = document.getElementById("button");
button.addEventListener("click", function(event){
   alert(event.target);
});
&#13;
<button id="button">
   <img src="http://www.iconarchive.com/download/i38829/google/chrome/Google-Chrome-Chromium.ico" />
</button>
&#13;
&#13;
&#13;

&#13;
&#13;
let buffer = document.querySelectorAll('.Tabs'),
        Tabs = [];

buffer.forEach(el => {
    Tabs.push({
        List: el,
        Switches: el.querySelectorAll('.TabsItem'),
        Contents: el.querySelectorAll('.TabsContentItem'),
    })
});
buffer = [];

Tabs.forEach(currentTab => {
        currentTab.Switches.forEach(tabSwitch => {
            tabSwitch.addEventListener('click', (e) => {
                e.stopPropagation();
                if (!tabSwitch.classList.contains('active')) {
                    currentTab.Switches.forEach(currentTabSwitchClear => { currentTabSwitchClear.classList.remove('active'); });
                    currentTab.Contents.forEach(currentTabContentClear => { currentTabContentClear.classList.remove('active'); });
                    tabSwitch.classList.add('active');
                    currentTab.Contents[Array.from(currentTab.Switches).indexOf(tabSwitch)].classList.add('active');
                }
            });
        });
    });
&#13;
.TabsContentItem {
  display: none;
}

.TabsContentItem.active {
  display: block;
}
&#13;
<div class="Tabs">
<button class="TabsItem active">
  <img src="https://static.licdn.com/sc/h/9wcfzhuisnwhyauwp7t9xixy7" />
 </button>
 <button class="TabsItem">
  <img src="https://www.askwoody.com/wp-content/themes/gear_askwoody/images/ico/home-icon.png" />
 </button>
 <div class="TabsContentItem active">
   Linkedin Tab
 </div>
  <div class="TabsContentItem">
   Home Tabs
 </div>
 </div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

这是使用CSS解决此问题的一种非常简单的方法。

如果您确定按钮内没有指针事件,则按钮内的单击将始终针对按钮本身:

button * {pointer-events: none;}

发件人: https://css-tricks.com/slightly-careful-sub-elements-clickable-things/