Metoda removeChild() DOM XML

Definicja i zastosowanie

removeChild() Metoda usuwania podwęzła.

Pomyślnie, funkcja zwraca usunięty węzeł, w przypadku błędu zwraca NULL

Gramatyka

elementNode.removeChild(node)
Parametry Opis
node Wymagane. Określa podwęzeł do usunięcia.

Przykład

Przykład 1

Poniższy kod ładuje "books.xml" do xmlDoc i usuwa podwęzły z pierwszego elementu <book>:

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 =
    "Usunięty węzeł: " + x.nodeName;
{}

Spróbuj sam

Przykład 2

Usunięcie ostatniego podelementu z listy węzłów:

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 =
    "Usunięty węzeł: " + x.nodeName;
{}

Spróbuj sam