XML DOM removeChild() method

Definition and usage

removeChild() Method to remove child nodes.

On success, the function returns the node removed, on failure it returns NULL.

Syntax

elementNode.removeChild(node)
Parameter Description
node Required. Specifies the child node to be removed.

Instance

Example 1

The following code loads "books.xml" into xmlDoc and removes the child nodes of the first <book> element:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var y = xmlDoc.getElementsByTagName("book")[0];
    var x = xmlDoc.documentElement.removeChild(y);
    document.getElementById("demo").innerHTML =
    已删除节点: " + x.nodeName;
}

亲自试一试

Example 2

From the node list, remove the last child node:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
    }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
    var xmlDoc = xml.responseXML;
    var len = xmlDoc.getElementsByTagName('book').length;
    var y = xmlDoc.getElementsByTagName("book")[len-1];
    var x = xmlDoc.documentElement.removeChild(y);
    document.getElementById("demo").innerHTML =
    已删除节点: " + x.nodeName;
}

亲自试一试