我有一个div:
<div id="myDiv">This Div is Tracked</div>
我需要一个函数来执行以下操作:
示例:
checkSeveralEvents = () => {
if (myDivIsHovered || myDivIsClicked || myDivIsFocus) {
console.log(`myDiv has been ${eventHere}`);
}
}
我该怎么做?
答案 0 :(得分:2)
您可以逐个收听所有需要的活动:
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;