XML DOM cloneNode()เมธอด
❮ วัตถุโหนด
ตัวอย่าง
โค้ดแฟรกเมนต์ต่อไปนี้โหลด " books.xml " โคลนโหนด <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 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;
}
ผลลัพธ์ของโค้ดด้านบนจะเป็น:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
ความหมายและการใช้งาน
cloneNode() วิธีการสร้างสำเนาของโหนด และส่งกลับโหนดโคลนใหม่
รองรับเบราว์เซอร์
cloneNode() วิธีการได้รับการสนับสนุนในเบราว์เซอร์หลักทั้งหมด
ไวยากรณ์
nodeObject.cloneNode(deep)
พารามิเตอร์
Parameter | Type | Description |
---|---|---|
deep | Boolean | true clones the node, its attributes, and its descendants.
false clones the node and its attributes. |
คืนมูลค่า
Type | Description |
---|---|
Node object | The cloned node |
รายละเอียดทางเทคนิค
เวอร์ชัน DOM | วัตถุโหนดหลักระดับ 1 |
---|
❮ วัตถุโหนด