我花了很多时间阅读有关内存泄漏的内容。我很困惑,因为对于 ie6 是正确的,对于 ie8 或更新的浏览器来说不再是正确的。据我了解,这段代码可能/将会泄漏,因为在一个函数中,我创建了一个 DOM 元素,我在该元素上绑定(bind)了一个事件。我的理解正确吗?如果是这样,注释中的代码也会泄漏吗?如果是,避免泄漏的最佳方法是什么?
function somefunc() {
var $CodeInstallation, $selInstallation;
$CodeInstallation = jQuery(<...some form tag...>);
$selInstallation = jQuery(
'<input value="select"' +
' type="button" name="selInstallation" ' +
' id="idSelInstallation"/>')
.appendTo($CodeInstallation.parent());
// should I do that instead ???
/*
jQuery('<input value="select"' +
' type="button" name="selInstallation" ' +
' id="idSelInstallation"/>')
.appendTo($CodeInstallation.parent());
$selInstallation = jQuery('#idSelInstallation');
*/
$selInstallation.click( function() {
alert('click!');
}); // click
}
最佳答案
感谢 Pointy 和 Kevin B。答案是否定的,因为 jQuery 处理事件的方式而不是附加到 DOM 对象。
关于javascript - 这段 JavaScript/jQuery 代码会在 ie8 或其他浏览器中泄漏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19910618/