• JavaScript 元素的删除


    在DOM操作中,有一个removeChild的方法。但是我们要知道,即使是这样从DOM中删除了,它们还是存在的。那它们会产生什么问题呢?一个严重的问题是,那些删除的节点都存在内存中,这就是传说中的内存泄露。如果几十兆,几百的时候就会影响浏览器的运行了。(- - 其实我是没有测试过的)

    所以我是知道,不能随便地删除元素节点。我之前用的是一个没什么用的方法。将它先设置display为node,再删除。感觉就是先把衣服脱掉再杀死你?

    JQ有一个remove方法,我觉得应该是考虑全面的,不知道有没有人告诉它会不会引起内存问题?

    在网上看到一个方法,就是动态创建一个空的元素,把要删除的节点丢到这个元素,再清空里面的内容,这样可以从内存中清除掉。

    var garbageBin;
    window.onload = function ()
        {
        if (typeof(garbageBin) === 'undefined')
            {
            //Here we are creating a 'garbage bin' object to temporarily 
            //store elements that are to be discarded
            garbageBin = document.createElement('div');
            garbageBin.style.display = 'none'; //Make sure it is not displayed
            document.body.appendChild(garbageBin);
            }
        function discardElement(element)
            {
            //The way this works is due to the phenomenon whereby child nodes
            //of an object with it's innerHTML emptied are removed from memory
    
            //Move the element to the garbage bin element
            garbageBin.appendChild(element);
            //Empty the garbage bin
            garbageBin.innerHTML = "";
            }
        }
    
    //To use it in your context, you would do it like this:
    
    discardElement(this);
  • 相关阅读:
    CS224N Assignment 1扩展阅读——词嵌入
    算法面经总结
    Leetcode刷题笔记(一)
    使用python-Statsmodels进行基于统计学的时间序列分析
    chinaunix book
    行政区划分
    视频网站
    sqlserver还原bak备份文件
    linux给终端设置代理
    ruby中exec,system,%x的区别
  • 原文地址:https://www.cnblogs.com/coolicer/p/2749450.html
Copyright © 2020-2023  润新知