Are event listeners in jQuery removed automaticall

2019-03-24 11:33发布

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?

3条回答
不美不萌又怎样
2楼-- · 2019-03-24 12:00

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())

查看更多
【Aperson】
3楼-- · 2019-03-24 12:01

Just to expand a bit:

So if you want to retain listeners, use .detach().

查看更多
Emotional °昔
4楼-- · 2019-03-24 12:14

Yeah, they will be removed even when you use html(). The jQuery source code confirms it.

查看更多
登录 后发表回答