Javascript 是否会删除已删除 DOM 元素的事件处理程序? [英] Does Javascript remove event handlers of deleted DOM elements?

查看:36
本文介绍了Javascript 是否会删除已删除 DOM 元素的事件处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将一堆事件附加到一些 DOM 元素 - 然后删除它们 - 用于它们的事件处理程序或其他属性的内存是否仍会被使用?

If I attached a bunch of events to some DOM elements - and then remove them - will the memory used for their event handlers or other attributes still be used?

我问是因为我想知道如果我不断用来自 AJAX 请求的新元素重新填充页面区域并将事件绑定到它们,我是否会使用一堆内存 - 只是删除它们并在新的AJAX 结果进来了.

I ask because I want to know if I will use a bunch of memory if I keep refilling an area of a page with new elements from AJAX requests and binding events to them - only to delete them and repeat the process when a new AJAX result comes in.

推荐答案

是的,现代浏览器(最终)会释放 DOM 节点中事件处理程序使用的内存.但是,旧版本的 Internet Explorer 没有,因此在从 DOM 中删除节点之前删除事件侦听器始终是一个好习惯.

Yes, modern browsers (eventually) release the memory used by event handlers in DOM nodes. However, old versions of Internet Explorer don't, so it's always good practice to remove event listeners before removing the nodes from the DOM.

这是一篇了解正在发生的事情的好文章:http://msdn.microsoft.com/en-us/library/bb250448(v=vs.85).aspx

This is a good article for understanding what's going on: http://msdn.microsoft.com/en-us/library/bb250448(v=vs.85).aspx

这篇关于Javascript 是否会删除已删除 DOM 元素的事件处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆