This question already has an answer here:
这个问题在这里已有答案:
- How to remove all listeners in an element? [duplicate] 3 answers
- 如何删除元素中的所有侦听器? [重复] 3个答案
Is it possible to remove all event listeners of an element and its children? Something like:
是否可以删除元素及其子元素的所有事件侦听器?就像是:
myElem.removeEventListeners();
I need this because I have a complex element with events, and I need to create a copy of it -- like a static image that does not react to any events.
我需要这个,因为我有一个复杂的事件元素,我需要创建它的副本 - 就像一个静态图像,不会对任何事件做出反应。
2 个解决方案
#1
22
If you use cloneNode
, the event listeners won't be copied.
如果使用cloneNode,则不会复制事件侦听器。
If you want a robust solution your best bet is probably to write a wrapper to attach/detach
listeners, and keep track of them yourself. Something like Dean Edwards' addEvent.
如果你想要一个强大的解决方案,最好的办法就是写一个包装器来连接/分离听众,并自己跟踪它们。像Dean Edwards的addEvent之类的东西。
#2
-8
I do not know of one, and a search on Google didn't yield any obvious results. One easy, kludgy solution is to create a function that cycles through every event and sets it to null
, such as
我不知道一个,谷歌的搜索没有产生任何明显的结果。一个简单易懂的解决方案是创建一个循环遍历每个事件并将其设置为null的函数,例如
function removeEvents(obj)
{
obj.onblur = null;
obj.onchange = null;
obj.onclick = null;
// ...
}
Call this function as follows: removeEvents(myElem)
. A complete list of JavaScript event handlers can be found at http://www.elated.com/articles/events-and-event-handlers/.
调用此函数如下:removeEvents(myElem)。有关JavaScript事件处理程序的完整列表,请访问http://www.elated.com/articles/events-and-event-handlers/。
Undoubtedly there is a more elegant way to write this function, but I don't know if there is a more elegant way to solve this problem. It would be lovely if someone knows of one. If you are binding events through a JavaScript library, such as jQuery, there may be other, better options.
毫无疑问,有一种更优雅的方式来编写这个函数,但我不知道是否有更优雅的方法来解决这个问题。如果有人知道,那将是可爱的。如果您通过JavaScript库(例如jQuery)绑定事件,则可能还有其他更好的选项。
#1
22
If you use cloneNode
, the event listeners won't be copied.
如果使用cloneNode,则不会复制事件侦听器。
If you want a robust solution your best bet is probably to write a wrapper to attach/detach
listeners, and keep track of them yourself. Something like Dean Edwards' addEvent.
如果你想要一个强大的解决方案,最好的办法就是写一个包装器来连接/分离听众,并自己跟踪它们。像Dean Edwards的addEvent之类的东西。
#2
-8
I do not know of one, and a search on Google didn't yield any obvious results. One easy, kludgy solution is to create a function that cycles through every event and sets it to null
, such as
我不知道一个,谷歌的搜索没有产生任何明显的结果。一个简单易懂的解决方案是创建一个循环遍历每个事件并将其设置为null的函数,例如
function removeEvents(obj)
{
obj.onblur = null;
obj.onchange = null;
obj.onclick = null;
// ...
}
Call this function as follows: removeEvents(myElem)
. A complete list of JavaScript event handlers can be found at http://www.elated.com/articles/events-and-event-handlers/.
调用此函数如下:removeEvents(myElem)。有关JavaScript事件处理程序的完整列表,请访问http://www.elated.com/articles/events-and-event-handlers/。
Undoubtedly there is a more elegant way to write this function, but I don't know if there is a more elegant way to solve this problem. It would be lovely if someone knows of one. If you are binding events through a JavaScript library, such as jQuery, there may be other, better options.
毫无疑问,有一种更优雅的方式来编写这个函数,但我不知道是否有更优雅的方法来解决这个问题。如果有人知道,那将是可爱的。如果您通过JavaScript库(例如jQuery)绑定事件,则可能还有其他更好的选项。