getAttribute方法

Definition and Usage
定义和用法

The getAttribute() method gets an attribute value by name.
getAttribute()方法获取指定名称所对应的属性值。
Syntax
语法

elementNode.getAttribute(name)


Parameter
参数         Description
描述
name         Required. Specifies the attribute to get the attribute value from
必要参数。指定获取名称所对应的属性值
  

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

The following code fragment gets the value of the "category" attribute in all <book> elements:
下面的代码片断将获取所有<book>元素中“category”的属性值:

xmlDoc=loadXMLDoc("books.xml");

var x=xmlDoc.getElementsByTagName(&apos;book&apos;);

for (i=0;i<x.length;i++)
  {
  document.write(x[i].getAttribute(&apos;category&apos;));
  document.write("<br />");
  }

 

getAttribute版本:DOM1 返回值:有

语法:
vAttrValue = object . getAttribute ( sAttrName , iFlags )
参数:

sAttrName : 必选项。字符串(String)。指定属性的名称。
iFlags : 可选项。整数值(Integer)。0 | 1 | 2
0 : 默认值。执行不考虑字母大小写的搜索,假如特性被找到了返回一个以内插值替换的值。假如 setAttribute 方法的 iFlags 参数设置为 1 而此方法的 iFlags 参数设置为 0 , 则满足 sAttrName 指定的特性名称不一定能被找到。
1 : 执行严格考虑字母大小写的搜索。
2 : 严格的按照脚本或源文档里的设置返回值。

返回值:

vAttrValue : 返回属性的值。可能是任意类型的变量。假如属性没有被呈递,则返回 null

说明:
获取指定属性的值。
假如对象有多个名字一样但字母大小写不同的属性,并且 iFlags 参数被设为 0 ,那么只有其中最后被建立的那一个会被此方法获取。而其他的则会被忽略。
当使用此方法获取 CLASS 属性的值时,需要将 sName 参数指定为 className 。这是 CLASS 属性所对应的 DHTML 特性。
此方法仅仅可以由从 HTML 组件建立的事件使用。

 

getAttribute(name)等于attributes.getNamedItem(name).value
 

posted @ 2009-05-07 09:44  翔宇编程  阅读(1546)  评论(0编辑  收藏  举报
51CTO