当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > setAttributeNode() 方法

XML DOM
DOM 节点
DOM 节点列表
DOM 解析
DOM 遍历节点树
DOM Mozilla 和 IE
DOM 获取节点
DOM 设置节点
DOM 删除节点
DOM 更换节点
DOM 建立节点
DOM 添加节点
DOM 克隆节点
DOM 节点类型
DOM Node
DOM NodeList
DOM NamedNodeMap
DOM Document
DOM DocumentType
DOM ProcessingInstr
DOM Element

XML DOM 中的 setAttributeNode() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 353 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The setAttributeNode() method adds a new attribute node.
setAttributeNode()方法的作用是:添加一个新的属性节点。

If an attribute with that name already exists in the element, it is replaced by the new one. If the new attribute replaces an existing attribute, the replaced attribute node is returned, otherwise it returns null.
如果一个元素中的属性名称已经存在,那么新添加的属性值将恢复改原来的值;如果新的属性值替代了原来的属性值,那么将返回被替代的属性节点,否则将返回null[空值]。

Syntax
语法

elementNode.setAttributeNode(att_node)

Parameter Description
描述
att_node Required. Specifies the attribute node to set
必要参数。指定需要设置的属性节点
 

In all examples, we will use the XML file books.xml, and the JavaScript function loadXMLDoc().
在所有案例中,我们将使用“book.xml”文件以及JavaScript 函数“loadXMLDoc()”。

Example
案例

The following code adds a "edition" attribute to all <book> elements in "books.xml":
下面的代码将把“edition”属性值添加到“books_ns.xml”文件中所有的<book>元素中:

xmlDoc=loadXMLDoc("books.xml");
var 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("<br />"); }

Output:
输出结果:

Edition: first
Edition: first
Edition: first
Edition: first


Try-It-Yourself Demos
自我演示

setAttributeNode() - Create and insert a new attribute node
setAttributeNode() - 创建并插入一个全新的属性节点

评论 (0) All

登陆 | 还没注册?