Metode XML DOM cloneNode()
Objek Simpul
Contoh
Fragmen kode berikut memuat " books.xml ", mengkloning simpul <book> pertama dan kemudian menambahkannya ke akhir daftar simpul:
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;
}
Output dari kode di atas akan menjadi:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
Definisi dan Penggunaan
Metode cloneNode() membuat salinan node, dan mengembalikan node clone yang baru.
Dukungan Peramban
Metode cloneNode() didukung di semua browser utama.
Sintaksis
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. |
Nilai Kembali
Type | Description |
---|---|
Node object | The cloned node |
Detail Teknis
Versi DOM | Objek Node Tingkat 1 Inti |
---|
Objek Simpul