在删除元素之前是否需要删除事件侦听器? [英] Do I need to remove event listeners before removing elements?
问题描述
如果我的子元素包含绑定了事件监听器的子元素,那么在清除父级之前是否需要删除这些事件监听器? (即 parent.innerHTML ='';
)如果事件侦听器从DOM中删除,如果从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屋!