XML DOM setAttributeNode() 方法

定義和用法
setAttributeNode() 方法添加新的屬性節點。
如果元素中已經存在指定名稱的屬性,那麼該屬性將被新屬性替代。如果新屬性替代了已有的屬性,則返回被替代的屬性節點,否則返回 NULL。
語法
elementNode.setAttributeNode(att_node)
參數 | 描述 |
---|---|
att_node | 必需。規定要設置的屬性節點。 |
實例
下麵的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,並向所有 <book> 元素添加 "edition" 屬性:
實例
xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
document.write("Edition: ");
document.write(x[0].getAttribute("edition"));
輸出:
Edition: first
