英文:
Is there any way to target two events together in addEventListener?
问题
我正在尝试使用JavaScript定义一些悬停属性。因此,我决定使用"mouseenter"和"mouseleave"事件来处理一个元素。现在,我需要在addEventListener中为这两个事件调用同一个函数。
所以,有没有办法这样做:
element.addEventListener("mouseenter"或"mouseleave",myfunc);
英文:
I am trying to define some hover properties using JavaScript. So, I decided to go with "mouseenter" and "mouseleave" events for an element. Now, I have to call the same function for both the events in addEventListener.
So, is there any way of doing it like:
element.addEventListener("mouseenter" or "mouseleave", myfunc);
答案1
得分: 1
如果你需要同时监听两个事件,那么你应该添加两个事件监听器,语法应该如下所示:
element.addEventListener("mouseenter", func);
element.addEventListener("mouseleave", func);
或者可以使用以下方式:
["mouseenter", "mouseleave"].forEach(event => {
element.addEventListener(event, func);
})
这两种方法是相同的。
英文:
Well if you need both events, then you should add two event listeners, so the syntax should look like this.
element.addEventListener("mouseenter", func);
element.addEventListener("mouseleave", func);
or
["mouseenter", "mouseleave"].forEach(event => {
element.addEventListener(event, func);
})
It's the same thing
答案2
得分: 0
你不能使用"or"来添加多个监听器,但你可以创建一个函数并将多个监听器添加到它上面。以下是一个示例:
function myfunc() {
console.log("事件被调用");
}
element.addEventListener("mouseenter", myfunc);
element.addEventListener("mouseleave", myfunc);
英文:
Well you cannot use an "or" to add multiple listeners but you can create one function and add multiple listeners to it. Here is an example:
function myfunc() {
console.log("Event is called");
}
element.addEventListener("mouseenter", myfunc);
element.addEventListener("mouseleave", myfunc);
答案3
得分: -1
对于这个确切的语法,你应该重写addEventListener
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
let _add;
[_add, HTMLElement.prototype.addEventListener] = [HTMLElement.prototype.addEventListener, function(name, ...args){
name.match(/\S+/g).forEach(name => _add.call(this, name, ...args));
}];
}
$test.addEventListener('mouseenter mouseleave', e => console.log(e.type));
<!-- language: lang-css -->
[id=$test]{
width:200px;
height:150px;
background:gray;
}
<!-- language: lang-html -->
<div id="$test"></div>
<!-- end snippet -->
一个不需要重写的一行代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
'mouseenter mouseleave'.split(' ').forEach(name => $test.addEventListener(name, e => console.log(e.type)));
<!-- language: lang-css -->
[id=$test]{
width:200px;
height:150px;
background:gray;
}
<!-- language: lang-html -->
<div id="$test"></div>
<!-- end snippet -->
英文:
For this exact syntax you should override the addEventListener
:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
let _add;
[_add, HTMLElement.prototype.addEventListener] = [HTMLElement.prototype.addEventListener, function(name, ...args){
name.match(/\S+/g).forEach(name => _add.call(this, name, ...args));
}];
}
$test.addEventListener('mouseenter mouseleave', e => console.log(e.type));
<!-- language: lang-css -->
[id=$test]{
width:200px;
height:150px;
background:gray;
}
<!-- language: lang-html -->
<div id="$test"></div>
<!-- end snippet -->
An one-liner without overriding:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
'mouseenter mouseleave'.split(' ').forEach(name => $test.addEventListener(name, e => console.log(e.type)));
<!-- language: lang-css -->
[id=$test]{
width:200px;
height:150px;
background:gray;
}
<!-- language: lang-html -->
<div id="$test"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论