DOM 节点列表


原文:http://www.w3pop.com/learn/view/doc/dom_nodes_nodelist/

DOM 节点列表

--------------------------------------------------------------------------------
作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-29 修改:2007-08-30 浏览:3258 ::  :: 
DOM 节点 DOM 解析  DOM NodeList and NamedNodeMap
DOM NodeList [节点列表]对象和 NamedNodeMap [指定节点映射]对象
This chapter explains what a NodeList is, and what a NamedNodeMap is. It also explains the differences between them.
通过本章的学习,你将学习什么是NodeList [ 节点列表 ],什么是NamedNodeMap[ 指定节点映射 ],以及它们之间存在着哪些不同。


--------------------------------------------------------------------------------

DOM NodeList
DOM NodeList [节点列表]对象
When using properties or methods like childNodes or getElementsByTagName(), we receive a NodeList object.
当你使用诸如 childNodes 或者 getElementsByTagName() 这样的属性或方法时,我们将获取一个NodeList [ 节点列表 ] 对象。

A NodeList object represents an ordered list of nodes.
一个NodeList [ 节点列表 ] 对象表示一个完整有序的节点列表。

The nodes in the NodeList can be accessed through their index number (starting from 0).
NodeList [ 节点列表 ] 中的节点可以通过它们的索引数字(从0开始)访问。

Note: In a NodeList, the nodes are returned in the order in which they are specified in the XML.
注意:在一个NodeList [ 节点列表 ] 中,节点是按照我们在XML中指定的顺序返回的。

先看看这个XML文件: books.xml

We will now create a node list of all the <title> elements in "books.xml", by using the getElementsByTagName("title") method. The following image represents the node list returned:
我们将使用getElementsByTagName("title")方法在“book.xml”文件中创建所有<title>元素节点列表。下面的图片展示了这个节点列表:



The following code fragment gets the text from the first <title> element:
下述代码片段将获取来自于第一个<title>元素的文本:

getElementsByTagName("title")[0].childNodes[0].nodeValue

Output:
输出结果:

Everyday Italian




--------------------------------------------------------------------------------

Get the Length of a Node List
获取节点列表长度
The node list keeps itself up-to-date. If an element is deleted or added, in the node list or the XML document, the list is automatically updated.
节点列表将会不断地进行自我更新。如果节点列表或XML文档中的一个元素被添加或删除,那么列表将自动更新。

The node list has a useful property: length. The length property returns the number of nodes in a node list.
节点列表包含一个非常实用的属性:length [ 长度 ]。length [ 长度 ] 属性返回了节点列表中的节点数量。

The following code fragment gets the number of <title> elements in "books.xml":
下述代码片段将获取“books.xml”文件中的<title>元素的数量:

getElementsByTagName('title').length

Output:
输出结果:

4

When you know the length of a node list, you can easily loop through it and extract the values you want.
当你知道了一个节点列表的长度时,你可以非常方便的对它执行循环语句并从中提取你想要的值。

The following code fragment loops through all <title> elements and prints their values:
下述代码片段将对<title>元素执行循环语句并输出它们的值:

//the x variable will hold a NodeListvar x=getElementsByTagName('title')for (i=0;i<x.length;i++)  {  document.write(x[i].childNodes[0].nodeValue)  document.write("<br />")  }

Output:
输出结果:

Everyday ItalianHarry PotterXQuery Kick StartLearning XML




--------------------------------------------------------------------------------

DOM NamedNodeMap
DOM NamedNodeMap [ 指定节点映射 ]
When using the attributes property on an element, we receive a NamedNodeMap object.
当对一个元素使用attribute属性时,我们将获取一个NamedNodeMap [ 指定节点映射 ] 对象。

A NamedNodeMap object represents an unordered list of attribute nodes.
一个 NamedNodeMap [ 指定节点映射 ] 对象代表一个无序的节点列表。

The nodes in the NamedNodeMap can be accessed through their name.
NamedNodeMap [ 指定节点映射 ] 对象中的节点可以通过它们的名称进行访问。

Note: In a NamedNodeMap, the nodes are not returned in any particular order.
注意:在一个NamedNodeMap [ 指定节点映射 ] 对象中,节点不会以任何一种特定的顺序返回。


--------------------------------------------------------------------------------

Get the Length of a NamedNodeMap
获取一个NamedNodeMap [ 指定节点映射 ] 对象的长度
The NamedNodeMap keeps itself up-to-date. If an element is deleted or added, in the node list or the XML document, the list is automatically updated.
NamedNodeMap [ 指定节点映射 ] 对象将会不断地进行自我更新。如果节点列表或XML文档中的一个元素被添加或是删除,那么列表将自动更新。

The NamedNodeMap also has a length property. The length property returns the number of nodes in the list.
NamedNodeMap [ 指定节点映射 ] 对象也包含一个length [ 长度 ] 属性。length [ 长度 ] 属性将返回列表中节点的数量。

请看看下面这个XML文件:books.xml

The following code fragment gets the number of attributes in the first <title> element in "books.xml":
下述代码片段将获取来自于第一个<title>元素的属性数量:

getElementsByTagName('title')[0].attributes.length

Output:
输出结果:

1




--------------------------------------------------------------------------------

Get an Item's Value in a NamedNodeMap
获取一个 NamedNodeMap [ 指定节点映射 ] 内的项值
The getNamedItem() method of the NamedNodeMap object can be used to retrieve a specified node.
NamedNodeMap [ 指定节点映射 ] 对象中的getNamedItem()方法可以用于获取一个指定的节点。

The following code fragment shows how to print the value of the "category" attribute in each <book> element":
下述代码片断将展示输出每个<book> 元素中“category”属性的值的方法:

xmlDoc=loadXMLDoc("books.xml");var x=xmlDoc.getElementsByTagName("book");for(i=0;i<x.length;i++)  {  //the attlist variable will hold a NamedNodeMap  var attlist=x.item(i).attributes;  var att=attlist.getNamedItem("category");  document.write(att.value + "<br />")  }

Output:
输出结果:

COOKING
CHILDRENWEBWEB






转载于:https://my.oschina.net/iqoFil/blog/221625

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值