關於jQuery的empty()方法和IE內存回收的一些體會

jQuery 的empty()方法在可以頁面移除元素,但是並不能釋放這些元素的子元素佔用的內存,而這些元素的子元素將變成“遊離狀態”,理解了樹的概念就很好理解這個。

現在需要做的:

第一步:

就是將這些子元素放進<div>標籤中,再將<div>的HTML賦值為空:

jQuery.fn.removeNode = function(){
        var d;
        return function(){
            if(this[0] && this[0].tagName != 'BODY'){
                d = d || document.createElement('div');
                d.appendChild(this[0]);
                d.innerHTML = '';
                d.outerHTML = '';
            }
        }
    }();

第二步:

將這些元素綁定的事件釋放

 (function($) {
        $.fn.Disposable = function(cln) {
            return this.each(function() {
                var el = this;
                if (!el.dispose) {
                    el.dispose = cleanup; // will be called by
                        // Microsoft for cleanup
                    $(window).bind("unload", cleanup);
                }
                function cleanup() {
                    if (!el)
                    return;
                    $(el).unbind();
                    $(window).unbind("unload", cleanup);
                    el.dispose = null;
                    el = null;
                };
            });
        };
    })(jQuery);

測試結果有效,可以通過 IE Sieve 工具進行查看。

以上jQuery代碼來自網絡,參考地址:

http://zhanjianhua.iteye.com/blog/230695#comments

http://www.codeproject.com/Articles/34348/jQuery-Memory-Leak-in-UpdatePanel

原文地址:https://www.cnblogs.com/wftrustself/p/3529763.html