从已删除的 DOM 元素分离 jQuery 侦听器

Detaching jQuery listeners from a deleted DOM element

删除 ID 为 #foo 的 DOM 元素是否会分离所有引用该 ID 的 jQuery 侦听器?例如,如果我在 $('#foo #bar') 上有一个侦听器,它是否也会自动删除? (我正在使用 elem.parentNode.removeChild(elem) 方法删除元素。)

如果我必须进行手动清理,是否使用像 $('#foo').off() 这样的 jQuery .off() 方法也会分离我使用 $('#foo #bar').on('click', myFunction) 附加的所有侦听器,或者这样做只分离?

Does deleting a DOM element with the id #foo detach all jquery listeners that reference that id?

如果您使用 jQuery 就可以,例如:

$("#foo").remove();

jQuery 为您处理清理工作。来自 the documentation:

In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

(我的重点)

之后,在示例删除 <div class="hello"> 之后:

If we had any number of nested elements inside <div class="hello">, they would be removed, too. Other jQuery constructs such as data or event handlers are erased as well.

(我的重点)