Phương thức XML DOM cloneNode ()
❮ Đối tượng nút
Thí dụ
Đoạn mã sau tải " books.xml ", sao chép nút <book> đầu tiên và sau đó thêm nó vào cuối danh sách nút:
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;
}
Đầu ra của đoạn mã trên sẽ là:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Everyday Italian
Định nghĩa và Cách sử dụng
Phương thức cloneNode () tạo một bản sao của một nút và trả về nút nhân bản mới.
Hỗ trợ trình duyệt
Phương thức cloneNode () được hỗ trợ trong tất cả các trình duyệt chính.
Cú pháp
nodeObject.cloneNode(deep)
Thông số
Parameter | Type | Description |
---|---|---|
deep | Boolean | true clones the node, its attributes, and its descendants.
false clones the node and its attributes. |
Giá trị trả lại
Type | Description |
---|---|
Node object | The cloned node |
Chi tiết kỹ thuật
Phiên bản DOM | Đối tượng nút cấp độ lõi 1 |
---|
❮ Đối tượng nút