addEventListener方法中有没有办法同时监听两个事件?

huangapple go评论63阅读模式
英文:

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 =&gt; _add.call(this, name, ...args));  
  }];
}

$test.addEventListener('mouseenter mouseleave', e =&gt; console.log(e.type));

<!-- language: lang-css -->

[id=$test]{
  width:200px;
  height:150px;
  background:gray;
}

<!-- language: lang-html -->

&lt;div id=&quot;$test&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

一个不需要重写的一行代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

'mouseenter mouseleave'.split(' ').forEach(name =&gt; $test.addEventListener(name, e =&gt; console.log(e.type)));

<!-- language: lang-css -->

[id=$test]{
  width:200px;
  height:150px;
  background:gray;
}

<!-- language: lang-html -->

&lt;div id=&quot;$test&quot;&gt;&lt;/div&gt;

<!-- 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 =&gt; _add.call(this, name, ...args));  
  }];
}

$test.addEventListener(&#39;mouseenter mouseleave&#39;, e =&gt; console.log(e.type));

<!-- language: lang-css -->

[id=$test]{
  width:200px;
  height:150px;
  background:gray;
}

<!-- language: lang-html -->

&lt;div id=&quot;$test&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

An one-liner without overriding:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&#39;mouseenter mouseleave&#39;.split(&#39; &#39;).forEach(name =&gt; $test.addEventListener(name, e =&gt; console.log(e.type)));

<!-- language: lang-css -->

[id=$test]{
  width:200px;
  height:150px;
  background:gray;
}

<!-- language: lang-html -->

&lt;div id=&quot;$test&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 20:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定