Javascript if多个事件的声明

时间:2017-05-14 10:46:20

标签: javascript ecmascript-6

我有一个div:

<div id="myDiv">This Div is Tracked</div>

我需要一个函数来执行以下操作:

示例:

checkSeveralEvents = () => {

    if (myDivIsHovered || myDivIsClicked || myDivIsFocus) {

        console.log(`myDiv has been ${eventHere}`);

    }

}

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您可以逐个收听所有需要的活动:

&#13;
&#13;
const d = document.getElementById('myDiv');
const events = ['click', 'mouseleave', 'mouseover'];

events.forEach(e => {
  d.addEventListener(e, (ev) => {
    console.log('My div has been ', e + 'ed')
  })
})
&#13;
<div id="myDiv">This Div is Tracked</div>
&#13;
&#13;
&#13;