简体   繁体   English

相同的 addEventListener 会起作用吗?

[英]Will the same addEventListener work?

elemen.addEventListener('click',func,false);
elemen.addEventListener('click',func,false);

Will whether the func be called twice on click on elemen ?点击elemen是否会调用func两次?

If yes... Is there a solution to check is the same event listener exists on elemen ?如果是...是否有解决方案来检查elemen上是否存在相同的事件侦听器?

In your example, func will not be called twice on click, no;在您的示例中, func在单击时不会被调用两次,不; but keep reading for details and a "gotcha."但请继续阅读以了解详细信息和“陷阱”。

From addEventListener in the spec:从规范中的addEventListener

If multiple identical EventListeners are registered on the same EventTarget with the same parameters the duplicate instances are discarded.如果在同一个EventTarget上使用相同的参数注册了多个相同的EventListeners ,则丢弃重复的实例。 They do not cause the EventListener to be called twice and since they are discarded they do not need to be removed with the removeEventListener method.它们不会导致EventListener被调用两次,并且由于它们被丢弃,因此不需要使用removeEventListener方法删除它们。

(My emphasis) (我的重点)

Here's an example:这是一个例子:

 var target = document.getElementById("target"); target.addEventListener("click", foo, false); target.addEventListener("click", foo, false); function foo() { var p = document.createElement("p"); p.innerHTML = "This only appears once per click, but we registered the handler twice."; document.body.appendChild(p); }
 <input type="button" id="target" value="Click Me">

It's important to note, though, that it has to be the same function , not just a function that does the same thing.不过,重要的是要注意,它必须是同一个函数,而不仅仅是一个做同样事情的函数。 For example, here I'm hooking up four separate functions to the element, all of which will get called:例如,在这里我将四个单独的函数连接到元素,所有这些函数都将被调用:

 var target = document.getElementById("target"); var count; for (count = 0; count < 4; ++count) { target.addEventListener("click", function() { var p = document.createElement("p"); p.innerHTML = "This gets repeated."; document.body.appendChild(p); }, false); }
 <input type="button" id="target" value="Click Me">

That's because on every loop iteration, a different function is created (even though the code is the same).这是因为在每次循环迭代中,都会创建一个不同的函数(即使代码相同)。

I would just like to add to the great answer provided by @TJ Crowler.我只想补充@TJ Crowler 提供的出色答案。

I had a specific task, that required me to add a same callback twice for the same event to an HTML element.我有一个特定的任务,要求我为同一个事件向 HTML 元素添加两次相同的回调。 It is true, that the second one discards the first, but:确实,第二个丢弃了第一个,但是:

If multiple identical EventListeners are registered on the same EventTarget with the same parameters, the duplicate instances are discarded.如果多个相同的 EventListener 在同一个 EventTarget 上注册了相同的参数,则重复的实例将被丢弃。 They do not cause the EventListener to be called twice, and they do not need to be removed manually with the removeEventListener() method.它们不会导致 EventListener 被调用两次,也不需要使用 removeEventListener() 方法手动删除。

Note, however that when using an anonymous function as the handler, such listeners will NOT be identical, because anonymous functions are not identical even if defined using the SAME unchanging source-code simply called repeatedly, even if in a loop.但是请注意,当使用匿名函数作为处理程序时,此类侦听器将不相同,因为即使使用相同不变的源代码定义,匿名函数也不相同,只是重复调用,即使在循环中也是如此。

Source: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners (as of 5th of February 2020.)来源: https ://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners(截至 2020 年 2 月 5 日。)

So if the second EventListener has its handler as an anonymous function it would not discard the first.因此,如果第二个 EventListener 将其处理程序作为匿名函数,它不会丢弃第一个。 So it would simply be called twice.所以它只会被调用两次。

An alternative for your loop solution.循环解决方案的替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM