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

XML DOM
DOM Attribute
DOM Text
DOM CDATA
DOM Comment
DOM HttpRequest
DOM ParseError
DOM 校验器
DOM 介绍
DOM 摘要
DOM 案例
DOM 节点树
DOM 访问节点树
DOM 节点信息
DOM 文档执行
DOM 节点导航

XML DOM 中的 setAttribute() 方法


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

Definition and Usage
定义和用法

The setAttribute() method adds a new attribute.
setAttribute() 方法的作用是添加一个新的属性。

If an attribute with that name already exists in the element, its value is changed to be that of the value parameter
如果一个元素中的属性名称已经存在,那么新添加的属性值将恢复改原来的值。

Syntax
语法

elementNode.setAttribute(name,value)

Parameter
参数
Description
描述
name Required. Specifies the name of the attribute to set
必要参数。指定需要设置的属性名称
value Required. Specifies the value of the attribute 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 an "edition" attribute to all <book> elements in "books.xml":
下面的代码将把“edition”属性值添加到“books.xml”文件中所有的<book>元素中:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("book");
for(i=0;i<x.length;i++) { x.item(i).setAttribute("edition","first"); }
//Output book title and edition value
var x=xmlDoc.getElementsByTagName("title");
for (i=0;i<x.length;i++) { document.write(x[i].childNodes[0].nodeValue); document.write(" - Edition: "); document.write(x[i].parentNode.getAttribute('edition')); document.write("<br />"); }

Output:
输出结果:

Everyday Italian - Edition: FIRST
Harry Potter - Edition: FIRST
XQuery Kick Start - Edition: FIRST
Learning XML - Edition: FIRST


Try-It-Yourself Demos
自我演示

setAttribute() - Change an attribute's value
setAttribute() - 改变一个属性值

setAttribute() - Create a new attribute and attribute value
setAttribute() - 创建一个新的属性(包括赋给它新的属性值)

评论 (0) All

登陆 | 还没注册?