jQuery:如果使用html()从DOM中删除事件处理程序,则从对象中删除它们 [英] jQuery : Are events handlers removed from objects if they are removed from the DOM using html()

查看:115
本文介绍了jQuery:如果使用html()从DOM中删除事件处理程序,则从对象中删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中担心内存泄漏,因为我使用jquery的html()方法来替代DOM中的内容。 我只想确保这些事件监听器不会在浏览器内存中挂起。



我已经搜索过jquery文档没有明确的答案。有人知道吗?



谢谢你们!

解决方案



如果您使用jQuerys .html(),它将会照顾您。查看jQuery源代码,该行被调用:

  jQuery.cleanData(this [i] .getElementsByTagName(* )); 

它有效地清理了所有的数据事件。如果您重新覆盖DOMnodes innerHTML 属性,这当然不起作用。


I am concerned about memory leaks in my application as I use jquery's html() method a lot to replace content in the the DOM. I just want to make sure that not of these event listeners are going to be hanging around in browser memory.

I have searched the jquery docs with no clear answer. Does anyone know?

Thanks guys!

解决方案

Yes they are.

If you use jQuerys .html() it will take care of you. Looking into the jQuery source, this line is getting called:

jQuery.cleanData( this[i].getElementsByTagName("*") );

which effectively cleans up all data and events. This of course won't work if you're overwritting a DOMnodes innerHTML property explicitly.

这篇关于jQuery:如果使用html()从DOM中删除事件处理程序,则从对象中删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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