XML DOM setAttributeNS() 方法


Element 对象参考手册 Element 对象

定义和用法

setAttributeNS() 方法添加新的属性(带有命名空间)。

如果元素中已经存在指定名称的属性或命名空间,它的值更改为前缀和 value 参数的值。

语法

elementNode.setAttributeNS(ns,name,value)
参数 描述
ns 必需。规定要设置的属性的命名空间 URI。
name 必需。规定要设置的属性的名称。
value 必需。规定要设置的属性的值。

实例 1

实例

xmlDoc=loadXMLDoc("books_ns.xml"); x=xmlDoc.getElementsByTagName("book")[0]; ns="/w3cnote/"; x.setAttributeNS(ns,"edition","first"); document.write(x.getAttributeNS(ns,"edition"));

输出:

first

运行代码 »

实例 2

实例

xmlDoc=loadXMLDoc("books_ns.xml"); x=xmlDoc.getElementsByTagName("title")[0]; ns="/w3cnote/"; x.setAttributeNS(ns,"c:lang","italian"); document.write(x.getAttributeNS(ns,"lang"));

输出:

italian

运行代码 »

Element 对象参考手册 Element 对象其他扩展