当使用.html()删除元素时,jQuery中的事件监听器是否自动删除? [英] Are event listeners in jQuery removed automatically when you remove the element using .html()?

查看:164
本文介绍了当使用.html()删除元素时,jQuery中的事件监听器是否自动删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery中,如果我们使用 .remove()删除某些元素,那么与元素关联的所有绑定事件和jQuery数据都将被删除。但是如果我们用 .html()删除元素会怎么样?



我们需要在更改任何html之前解除绑定所有元素以避免内存泄漏?

解决方案

是的,他们将被删除。 jQuery将清除与删除的元素相关的事件等。如果您执行像$(elm1).html($ elm2.html())


,则不会复制事件

In jQuery if we use .remove() for removing some element, then all bound events and jQuery data associated with the elements are removed.

But what happens if we "remove" the elements with .html()?

Do we need to unbind all the elements prior to change any html for avoiding memory leaks?

解决方案

Yes, they will be removed. jQuery will clean up events etc related to the removed elements. It will NOT copy events if you do something like $(elm1).html($elm2.html())

这篇关于当使用.html()删除元素时,jQuery中的事件监听器是否自动删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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