Phương thức appendChild() gắn thêm nút con mới vào cuối danh sách nút con của một nút.
Lưu ý: Nếu newchild đã có trong cây nào đó, thì nó sẽ được tách khỏi vị trí hiện tại của nó và gắn qua vị trí mới.
Cú pháp
nodeObject.appendChild(newchild)
| Tham số | Mô tả |
|---|---|
| newchild | Một nút con sẽ được thêm (gắn vào) nodeObject |
Giá trị trả về
| Kiểu | Mô tả |
|---|---|
| Đối tượng nút | Nút được nối thêm |
Ví dụ
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 newel = xmlDoc.createElement("edition");
var x = xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.getElementById("demo").innerHTML =
x.getElementsByTagName("edition")[0].nodeName;
}
Đầu ra của đoạn mã trên sẽ là:
edition
Ví dụ 2:
// Create a new paragraph element, and append it to the end of the document body var p = document.createElement("p"); document.body.appendChild(p);






