XML DOM removeAttributeNode() 方法
❮ 元素物件
示例
以下程式碼片段載入 "books.xml" 到 xmlDoc 中,並移除所有 <book> 元素上的 "category" 屬性節點。
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;
}
輸出
Removed: category: cooking
Removed: category: children
Removed: category: web
Removed: category: web
Removed: cover: paperback
自己動手試一試 »
定義和用法
removeAttributeNode() 方法移除指定的屬性節點。
如果 DTD 中定義了該屬性的預設值,則會立即出現一個具有預設值的新屬性。
此函式返回被移除的屬性節點。
語法
elementNode.removeAttributeNode(node)
引數 | 描述 |
---|---|
node | 必需。要移除的節點 |
❮ 元素物件