清除 dom 节点子节点时自动删除事件处理程序
有没有办法内省 dom 节点以查看是否附加了事件处理程序,以便您可以有效地编写一个安全函数来清除 dom 节点,而不必担心事件处理程序留下的内存泄漏?我希望以通用的方式做到这一点。
Is there a way to introspect a dom node to see if there are event handlers attached, so you can effectively write a safe function to clear out the dom nodes without worrying about memory leaks left by event handlers? I was hoping to do this in a generic fashion.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,这可能是不可能的,这是来自 mozilla 网站的引用:
https ://developer.mozilla.org/en/DOM/element.addEventListener#Memory_issues
如果听众不是匿名的,你可以这样做。这是来自 YUI 库事件的一段代码:
您可以在此处阅读更多内容:
http://developer.yahoo.com/yui/event/
From what I see it might not be possible, here is a quote from mozilla site:
https://developer.mozilla.org/en/DOM/element.addEventListener#Memory_issues
if the listener is not anonymous you could do it. Here is a piece of code from YUI library Event:
you can read more here:
http://developer.yahoo.com/yui/event/
这取决于。可以有效地删除由
el.onclick = ...
等属性分配的简单事件处理程序,但在 IE 中没有通过attachEvent()
添加的处理程序列表。 内存泄漏在其他浏览器中并不是什么大问题。如果您想处理这两种情况并管理页面中的所有事件处理程序,您可以在自己的函数中包装
addEvent
功能,并在要删除元素时删除它们。It depends. Simple event handlers assigned by property like
el.onclick = ...
can be effectively removed, but there is no list of handlers added viaattachEvent()
in IE. Memory leaks are not much of a concern in other browsers.If you want to handle both cases and you manage all event handlers in your page you can wrap
addEvent
functionality in your own function, and delete them when an element is to be removed.