【发布时间】:2018-08-16 04:42:35
【问题描述】:
假设你有这个 html 代码:
<button id="click-me" type="button">Click Me!</button>
然后你运行这个 jQuery sn-p:
var button = $('#click-me');
button.on('click', function() {
console.log('Clicked !');
});
button.replaceWith('<button id="click-me" type="button">Click Me (Replaced)!</button>');
当然该按钮不会做任何事情,因为它被另一个没有绑定点击事件处理程序的按钮替换,对吧?。
我的问题是: click 事件处理程序是否仍然存在于内存中的某处?或者,它是否被垃圾收集过程删除了?
【问题讨论】:
-
为replaceWith定义的JQuery API:
The .replaceWith() method removes all data and event handlers associated with the removed nodes. -
@Sphinx 非常感谢,这样我就不用担心内存泄漏了?
标签: javascript jquery memory