XML-DOM -CloneNode()- Methode
❮ Knotenobjekt
Beispiel
Das folgende Codefragment lädt „ books.xml “, klont den ersten <book>-Knoten und fügt ihn dann am Ende der Knotenliste hinzu:
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 x, y, cloneNode, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book')[0];
cloneNode
= x.cloneNode(true);
xmlDoc.documentElement.appendChild(cloneNode);
//
Output all titles
y = xmlDoc.getElementsByTagName("title");
for (i = 0; i < y.length; i++) {
txt += y[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
Die Ausgabe des obigen Codes lautet:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
Definition und Verwendung
Die Methode cloneNode() erstellt eine Kopie eines Knotens und gibt den neu geklonten Knoten zurück.
Browser-Unterstützung
Die Methode cloneNode() wird in allen gängigen Browsern unterstützt.
Syntax
nodeObject.cloneNode(deep)
Parameter
Parameter | Type | Description |
---|---|---|
deep | Boolean | true clones the node, its attributes, and its descendants.
false clones the node and its attributes. |
Rückgabewert
Type | Description |
---|---|
Node object | The cloned node |
Technische Details
DOM-Version | Knotenobjekt der Kernebene 1 |
---|
❮ Knotenobjekt