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

时间:2013-11-11 15:58:59

标签: javascript jquery memory-leaks internet-explorer-8

我花了很多时间阅读内存泄漏。我很困惑,因为对于ie8或更新的浏览器来说,ie6的真实情况不再适用。根据我的理解,这段代码可能/会泄漏,因为在一个函数中,我创建了一个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
}

1 个答案:

答案 0 :(得分:0)

感谢Pointy和Kevin B.答案是否定的,因为jQuery处理事件而不是附加到DOM对象的方式。