Phương pháp removeChild() của XML DOM
Định nghĩa và cách sử dụng
removeChild()
Phương pháp xóa nút con.
Khi thành công, hàm này trả về nút đã xóa, khi thất bại thì trả về NULL
。
Cú pháp
elementNode.removeChild(node)
Tham số | Mô tả |
---|---|
node | Bắt buộc. Định nghĩa nút con cần xóa. |
Mô hình
Ví dụ 1
Mã dưới đây sẽ tải "books.xml" vào xmlDoc và xóa các nút con của phần tử đầu tiên <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 xmlDoc = xml.responseXML; var y = xmlDoc.getElementsByTagName("book")[0]; var x = xmlDoc.documentElement.removeChild(y); document.getElementById("demo").innerHTML = "Xóa nút: " + x.nodeName; {}
Ví dụ 2
Từ danh sách các nút xóa nút con cuối cùng:
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 len = xmlDoc.getElementsByTagName('book').length; var y = xmlDoc.getElementsByTagName("book")[len-1]; var x = xmlDoc.documentElement.removeChild(y); document.getElementById("demo").innerHTML = "Xóa nút: " + x.nodeName; {}