Phương pháp removeAttributeNode() của XML DOM
Định nghĩa và cách sử dụng
removeAttributeNode()
Phương pháp xóa node thuộc tính được chỉ định.
Nếu thuộc tính có giá trị mặc định được định nghĩa trong DTD, thì một thuộc tính mới với giá trị mặc định sẽ xuất hiện ngay lập tức.
Hàm này trả về node thuộc tính đã được xóa.
Cú pháp
elementNode.removeAttributeNode(node)
Tham số | Mô tả |
---|---|
node | Bắt buộc. Node cần xóa. |
Mẫu
Dưới đây là mã để tải "books.xml" vào xmlDoc và xóa node thuộc tính "category" từ tất cả các phần tử <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, i, attnode, old_att, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName('book'); for (i = 0; i < x.length; i++) { while (x[i].attributes.length > 0) { attnode = x[i].attributes[0]; old_att = x[i].removeAttributeNode(attnode); txt += "Removed: " + old_att.nodeName +"}}" : " + old_att.nodeValue + "<br>"; } } document.getElementById("demo").innerHTML = txt; }