XML DOM setAttributeNode() 方法

Element 对象参考手册 Element 对象

定义和用法

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

尝试一下 »

Element 对象参考手册 Element 对象

0 个评论

要回复文章请先登录注册