这个JavaScript / jQuery代码是否在ie8或其他浏览器中泄漏

Does this JavaScript / jQuery code leak in ie8 or other browsers?

本文关键字:其他 浏览器 泄漏 ie8 JavaScript jQuery 代码 是否 这个      更新时间:2023-09-26

我花了很多时间阅读有关内存泄漏的信息。我很困惑,因为对于 ie6 来说是正确的,对于 ie8 或更新的浏览器来说不再是真的。据我了解,这段代码可能会/会泄漏,因为在函数中,我创建了一个 DOM 元素,我在其上绑定了一个事件。我的理解正确吗?如果是这样,注释中的代码也会泄漏吗?如果是这样,不泄漏的最佳方法是什么?

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对象的方式。

相关文章: