陈童的博客's Archivers

From everyinch on 2014-01-26 08:40:24

ADS3.5 DOM2核心和DOM2 HTML——Node对象之三:节点的属性

作为核心Attr对象的实例,节点的属性被包含在相应节点的attributes成员的一个NamedNodeMap对象中。下图展示了DOM核心表示attribute节点关系的方式:
<img src="http://www.everyinch.net/wp-content/uploads/2014/01/blog_attribute.jpg" alt="blog_attribute" width="700" height="537" class="alignnone size-full wp-image-4610" />
上图中的attributes属性,可以通过锚的attributes属性来访问:
[code lang="js"]
ADS.addEvent(window, 'load', function() {
ADS.log.header('Attributes');
var firefoxAnchor = document.getElementById('firefox');
for(var i=0 ; i < firefoxAnchor.attributes.length ; i++) {
ADS.log.write(
firefoxAnchor.attributes.item(i).nodeName
+ ' = '
+ firefoxAnchor.attributes.item(i).nodeValue
);
}
});
[/code]
在日志窗口的输出为:
□ id = firefox
□ title = Get Firefox
□ href = http://www.getfirefox.com/
Attr节点中还包括一个Text子节点,这个Text节点包含着与nodeValue相同的值。
与NodeList对象类似,NamedNodeMap对象中的项也可以使用方括号语法来访问:
[code lang="js"]
firefoxAnchor.attributes[i].nodeName
[/code]
另外,还可以通过getNanmedItem()方法获得指向具体属性节点的引用:
[code lang="js"]
var link = firefoxAnchor.attributes.getNamedItem('href').nodeValue;
[/code]
getNamedItem()方法与Element对象的getAttribute()方法类似,不过attributes.getNamedItem()方法在任何节点上都有效,也包括那些不是Element对象的节点。

查看完整版本: ADS3.5 DOM2核心和DOM2 HTML——Node对象之三:节点的属性

Tags:


©陈童的博客