我需要删除事件侦听器吗? [英] Do I need to remove event listeners?
问题描述
如果我有一个带有孩子的父元素,其中有事件侦听器绑定到它们,我需要删除那些事件侦听器才能清除父进程? (即 parent.innerHTML ='';
)如果事件侦听器从DOM元素中移除时未绑定事件侦听器,那么是否存在内存泄漏?
If I have a parent element with children who have event listeners bound to them, do I need to remove those event listeners before I clear the parent? (i.e., parent.innerHTML = '';
) Could there be memory leaks if event listeners are not unbound from an element if it's removed from the DOM?
推荐答案
简答:是
浏览器可以正确处理,并自己删除这些处理程序。有一些旧的浏览器(IE 6和7,如果我记得正确),这是搞砸了。是的,可能会有内存泄漏。你不必担心这一点,但你需要。请查看此文档。
Long answer: Most browsers handle this correctly and remove those handlers themselves. There are some older browsers (IE 6 and 7, if i recall correctly) that are messing this up. Yes, there could be memory leaks. You should not have to worry about this, but you need to. Have a look at this document.
这篇关于我需要删除事件侦听器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!