Delete JavaScript elements

Source: Internet
Author: User

In DOM operations, there is a removeChild method. But we need to know that even if they are deleted from the DOM in this way, they still exist. What problems will they cause? A serious problem is that all deleted nodes exist in the memory, which is the legendary Memory leakage. If dozens of megabytes are used, the operation of the browser may be affected in a few hundred times. (-Actually, I have never tested it)

So I know that you cannot delete element nodes at will. I used a useless method before. Set display to node and then delete it. I feel like I took off my clothes before killing you?

JQ has a remove method. I think it should be comprehensive. I don't know if anyone tells it whether it will cause memory problems?

You can see a method on the Internet, that is, dynamically create an empty element, drop the node to be deleted, and then clear the content in the element, so that the content can be removed from the memory.

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.