getAttributeNode() 方法從當(dāng)前元素中通過名稱獲取屬性節(jié)點(diǎn)。
elementNode.getAttributeNS(ns,name)
參數(shù) | 描述 |
---|---|
name | 必需。規(guī)定要獲取的屬性節(jié)點(diǎn)。 |
該方法將返回一個(gè) Attr 節(jié)點(diǎn),表示指定的屬性和值。請(qǐng)注意,通過從 Node 接口繼承的 attributes 屬性也可以獲取該屬性節(jié)點(diǎn)。
在所有的例子中,我們將使用 XML 文件 books.xml,以及 JavaScript 函數(shù) 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")
;
document.write(attnode.name);
document.write(" = ");
document.write(attnode.value);
document.write("<br />");
}
以上代碼的輸出:
category = COOKING category = CHILDREN category = WEB category = WEB
如對(duì)本文有疑問,請(qǐng)?zhí)峤坏浇涣髡搲瑥V大熱心網(wǎng)友會(huì)為你解答?。?點(diǎn)擊進(jìn)入論壇