删除匿名活动听众
无论如何,是否有添加这样的事件侦听器:
element.addEventListener(event, function(){/* do work here */}, false);
不替换元素?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
无论如何,是否有添加这样的事件侦听器:
element.addEventListener(event, function(){/* do work here */}, false);
不替换元素?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
除非您在Creation中存储了对事件处理程序的引用,否则无法干净地删除事件处理程序。
我通常会将它们添加到该页面上的主要对象中,然后您可以在使用该对象时迭代并清洁处理它们。
There is no way to cleanly remove an event handler unless you stored a reference to the event handler at creation.
I will generally add these to the main object on that page, then you can iterate and cleanly dispose of them when done with that object.
您可以这样删除活动听众:
You could remove the event listener like this:
旧问题,但这是一个解决方案。
严格来说,除非您存储对函数的引用,否则您不能删除匿名事件侦听器。由于使用匿名函数的目标可能不是创建一个新变量,因此您可以将参考存储在元素本身中:
稍后,当您要删除它时,您可以执行以下操作:
请记住,第三个参数(<代码> false )必须具有与添加事件侦听器相同的 值。
但是,问题本身乞求另一个:为什么?
使用
.addeventListener()
而不是简单的.onmetthing()
方法:首先,它允许多>多个事件侦听器是额外。当涉及到选择性地删除它们时,这将成为一个问题:您可能最终会命名它们。如果您想全部删除它们,则 @Tidy-Giant的
OuterHtml
解决方案非常好。其次,您确实可以选择选择捕获而不是冒泡事件。
如果这两种原因都不重要,您可能会决定使用更简单的
Onsomething
方法。Old Question, but here is a solution.
Strictly speaking you can’t remove an anonymous event listener unless you store a reference to the function. Since the goal of using an anonymous function is presumably not to create a new variable, you could instead store the reference in the element itself:
Later, when you want to remove it, you can do the following:
Remember, the third parameter (
false
) must have the same value as for adding the Event Listener.However, the question itself begs another: why?
There are two reasons to use
.addEventListener()
rather than the simpler.onsomething()
method:First, it allows multiple event listeners to be added. This becomes a problem when it comes to removing them selectively: you will probably end up naming them. If you want to remove them all, then @tidy-giant’s
outerHTML
solution is excellent.Second, you do have the option of choosing to capture rather than bubble the event.
If neither reason is important, you may well decide to use the simpler
onsomething
method.匿名 bount 事件侦听器
删除所有事件侦听器的最简单方法是将其
exturalhtml
分配给本身。这样做的是通过HTML解析器发送HTML的字符串表示,并将解析的HTML分配给元素。因为没有通过JavaScript传递,所以将无绑定的事件听众。匿名委派事件听众
一个警告是委派的事件听众,或者在父元素上的事件听众,观察每个事件的每个事件都符合其孩子的一组标准。超越的唯一方法是改变元素以不符合委派事件听众的标准。
Anonymous bound event listeners
The easiest way to remove all event listeners for an element is to assign its
outerHTML
to itself. What this does is send a string representation of the HTML through the HTML parser and assign the parsed HTML to the element. Because no JavaScript is passed, there will be no bound event listeners.Anonymous delegated event listeners
The one caveat is delegated event listeners, or event listeners on a parent element that watch for every event matching a set of criteria on its children. The only way to get past that is to alter the element to not meet the criteria of the delegated event listener.
是的,您可以删除一个匿名事件侦听器:
然后,您以这样的方式删除事件侦听器:
Yes you can remove an anonymous event listener:
You then remove the event listener like this:
您可以尝试覆盖
element.AddeventListener
并做任何您想做的事情。类似:
PS。:不建议这样做,但它会做到这一点(尚未对其进行测试)
You may try to overwrite
element.addEventListener
and do whatever you want.Something like:
ps.: it is not recommended, but it will do the trick (haven't tested it)
用字面函数分配事件处理程序很棘手 - 不仅没有任何方法可以删除它们,而不会克隆节点并用克隆替换它 - 您也可以多次无意中分配同一处理程序,如果您使用使用引用处理程序。即使角色相同,两个函数也总是被视为两个不同的对象。
Assigning event handlers with literal functions is tricky- not only is there no way to remove them, without cloning the node and replacing it with the clone- you also can inadvertantly assign the same handler multiple times, which can't happen if you use a reference to a handler. Two functions are always treated as two different objects, even if they are character identical.
编辑: as manngo 建议每个评论,您应该使用 .off()< /strong>而不是 .unbind() as .unbind()被弃用为jQuery 3.0,自JQuery 1.7以来就取代了。
即使这是一个古老的问题,也没有提及jQuery,我也会在这里发布答案,因为这是搜索术语'jQuery删除匿名事件处理程序'的第一个结果。
您可以尝试使用。off()函数来删除它。
但是,仅当您使用jQuery添加了侦听器时,这才有效-NOT 。addeventListener
找到此。
Edit: As Manngo suggested per comment, you should use .off() instead of .unbind() as .unbind() is deprecated as of jQuery 3.0 and superseded since jQuery 1.7.
Even though this an old question and it does not mention jQuery I will post my answer here as it is the first result for the searchterm 'jquery remove anonymous event handler'.
You could try removing it using the .off() function.
However this only works if you've added the listener using jQuery - not .addEventListener
Found this here.
如果您使用的是jQuery尝试
off
方法If you're using jQuery try
off
methodjQuery .off()方法删除了随附.on()附加的事件处理程序
Jquery .off() method removes event handlers that were attached with .on()
使用Ecmascript2015(ES2015,ES6)语言规范,可以使用此
nameandselfbind
函数,它神奇地将匿名回调变成一个指定的回调,甚至可以将其身体绑定到本身,从而使事件听众可以删除删除本身也可以从外部范围中删除(With ECMAScript2015 (ES2015, ES6) language specification, it is possible to do with this
nameAndSelfBind
function that magically turns an anonymous callback into a named one and even binds its body to itself, allowing the event listener to remove itself from within as well as it to be removed from an outer scope (JSFiddle):结尾:)
我在Chrome 92中测试,工作
编辑:仅在Chrome中工作,
因为
getEventListeners
Chrome中可用的功能。THE END :)
i test in chrome 92, worked
Edit: Work Only in Chrome
because
getEventListeners
function available in Chrome.我如何为我的CustomeVent使用选项参数
作为我创建的自定义功能,它效果很好。
检查了我的控制台,似乎有效(任何反馈都赞赏!)
How I used options parameter for my customEvent
for my custom function that I created, it worked quite nicely.
checked my console, seems like it worked (any feedback appreciated!)
outerText
答案确实做得很好,但是的问题也将从所有子节点中删除听众。为了解决这个问题,您应该先存储对节点的引用,清除元素,然后在替换offerText
属性后将其添加回。以下将从身体元素中删除所有事件听众。
The
outerText
answer does this really well but has the issue that it will also remove the listeners from all child nodes too. To get around this, you should store a reference to the nodes first, clear the element, then add them back after you've replaced theouterText
property.The following will remove all event listeners from the body element.
以下对我来说足够好。该代码处理另一个事件触发侦听器从元素中删除的情况。事先无需函数声明。
The following worked well enough for me. The code handles the case where another event triggers the listener's removal from the element. No need for function declarations beforehand.