XML DOM removeAttributeNode() 方法

定義和用法

removeAttributeNode() 方法從元素中刪除指定的屬性節點。

語法:

elementNode.removeAttributeNode(node)
參數 描述
node 必需。要刪除的節點。

返回值

刪除的 Attr 節點。

說明

該方法從當前元素的屬性集合中刪除(并返回)一個 Attr 節點。如果 DTD 給刪除的屬性設置了默認值,那么該方法將添加一個新的 Attr 節點,表示這個默認值。用 removeAttribute() 方法代替該方法往往會更簡單。

實例

在所有的例子中,我們將使用 XML 文件 books.xml,以及 JavaScript 函數 loadXMLDoc()

下面代碼片段從 "books.xml" 中的所有 <book> 元素中刪除 "category" 屬性:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
for(i=0;i<x.length;i++)
{
attnode=x.item(i).getAttributeNode("category");
old_att=x.item(i).removeAttributeNode(attnode);
document.write("Removed attribute: " + old_att.name + "<br />");
}

輸出:

Removed attribute: category
Removed attribute: category
Removed attribute: category
Removed attribute: category