How can I remove a child node in HTML using JavaScript?

前端 未结 9 1392
野的像风
野的像风 2020-12-12 20:22

Is there a function like document.getElementById(\"FirstDiv\").clear()?

相关标签:
9条回答
  • 2020-12-12 20:41

    Use the following code:

    //for Internet Explorer
    document.getElementById("FirstDiv").removeNode(true);
    
    //for other browsers
    var fDiv = document.getElementById("FirstDiv");
    fDiv.removeChild(fDiv.childNodes[0]); //first check on which node your required node exists, if it is on [0] use this, otherwise use where it exists.
    
    0 讨论(0)
  • 2020-12-12 20:43

    You have to remove any event handlers you've set on the node before you remove it, to avoid memory leaks in IE

    0 讨论(0)
  • 2020-12-12 20:51

    To answer the original question - there are various ways to do this, but the following would be the simplest.

    If you already have a handle to the child node that you want to remove, i.e. you have a JavaScript variable that holds a reference to it:

    myChildNode.parentNode.removeChild(myChildNode);
    

    Obviously, if you are not using one of the numerous libraries that already do this, you would want to create a function to abstract this out:

    function removeElement(node) {
        node.parentNode.removeChild(node);
    }
    

    EDIT: As has been mentioned by others: if you have any event handlers wired up to the node you are removing, you will want to make sure you disconnect those before the last reference to the node being removed goes out of scope, lest poor implementations of the JavaScript interpreter leak memory.

    0 讨论(0)
  • 2020-12-12 20:56
        var p=document.getElementById('childId').parentNode;
        var c=document.getElementById('childId');
        p.removeChild(c);
        alert('Deleted');
    

    p is parent node and c is child node
    parentNode is a JavaScript variable which contains parent reference

    Easy to understand

    0 讨论(0)
  • 2020-12-12 20:57

    If you want to clear the div and remove all child nodes, you could put:

    var mydiv = document.getElementById('FirstDiv');
    while(mydiv.firstChild) {
      mydiv.removeChild(mydiv.firstChild);
    }
    
    0 讨论(0)
  • 2020-12-12 20:57

    A jQuery solution

    HTML

    <select id="foo">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    

    Javascript

    // remove child "option" element with a "value" attribute equal to "2"
    $("#foo > option[value='2']").remove();
    
    // remove all child "option" elements
    $("#foo > option").remove();
    

    References:

    Attribute Equals Selector [name=value]

    Selects elements that have the specified attribute with a value exactly equal to a certain value.

    Child Selector (“parent > child”)

    Selects all direct child elements specified by "child" of elements specified by "parent"

    .remove()

    Similar to .empty(), the .remove() method takes elements out of the DOM. We use .remove() when we want to remove the element itself, as well as everything inside it. In addition to the elements themselves, all bound events and jQuery data associated with the elements are removed.

    0 讨论(0)
提交回复
热议问题