gpt4 book ai didi

Javascript if 语句用于多个事件

转载 作者:行者123 更新时间:2023-12-03 04:20:40 25 4
gpt4 key购买 nike

我有一个 div:

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

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

示例:

checkSeveralEvents = () => {

if (myDivIsHovered || myDivIsClicked || myDivIsFocus) {

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

}

}

我该怎么做?

最佳答案

您可以一一监听所有需要的事件:

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')
})
})
<div id="myDiv">This Div is Tracked</div>

关于Javascript if 语句用于多个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43962977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com