XML DOM setAttributeNode() 方法
Element 對(duì)象
定義和用法
setAttributeNode() 方法添加新的屬性節(jié)點(diǎn)。
如果元素中已經(jīng)存在指定名稱的屬性,那么該屬性將被新屬性替代。如果新屬性替代了已有的屬性,則返回被替代的屬性節(jié)點(diǎn),否則返回 NULL。
語法
elementNode.setAttributeNode(att_node)
參數(shù) | 描述 |
att_node | 必需。規(guī)定要設(shè)置的屬性節(jié)點(diǎn)。 |
實(shí)例
下面的代碼片段使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中,并向所有 <book> 元素添加 "edition" 屬性:
實(shí)例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
var newatt;
for (i=0;i<x.length;i++)
{
newatt=xmlDoc.createAttribute("edition");
newatt.value="first";
x[i].setAttributeNode(newatt);
}
//Output all "edition" attribute values
for (i=0;i<x.length;i++)
{
document.write("Edition: ");
document.write(x[i].getAttribute("edition"));
document.write("
");
}
輸出:
Edition: first
Edition: first
Edition: first
Edition: first
嘗試一下 ?
Element 對(duì)象
更多建議: