当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > firstChild 属性

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 中的 firstChild 属性


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

Definition and Usage
定义和用法

The lastChild property returns the last child node of the selected element
lastChild 属性的作用是:返回已选元素中的最后一个子节点。

If the selected node has no children, this property returns NULL.
如果已选节点不包含子节点。那么这个属性将返回NULL[空值]。

Syntax
语法

elementNode.lastChild
 

Tips and Notes
提示和注意点

Note: Internet Explorer will skip white-space text nodes that are generated between nodes (e.g. new-line characters), while Mozilla will not. So, in the example below, we have a function that checks the node type of the last child node.
注意点:注意:IE将跳过在节点之间产生的空格文档节点(如:换行字符),而Mozilla不会这样。在下面的案例中,我们将书写一个用于检测第一个子节点的节点类型的函数。

Element nodes has a nodeType of 1, so if the last child node is not an element node, it moves to the previous node, and checks if this node is an element node. This continues until the last child node (which must be an element node) is found. This way, the result will be correct in both Internet Explorer and Mozilla.
元素节点包含的节点类行为1,因此,如果最后一个节点不是元素节点,那么它将自动移动到上一个节点并检测该节点是否是一个元素节点;这样的过程将一直持续下去直到发现的最后一个子节点是元素节点为止。通过使用这个方法,在IE浏览器或Mozilla浏览器中显示的结果就会同时正确。

Tip: To read more about the XML DOM differences between IE and Mozilla browsers, visit our Mozilla vs. IE chapter.
提示:如果你想获取更多关于XML DOM在IE和Mozilla浏览器中的不同,那你可以访问“Mozilla vs. IE ”这章。


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 last child node from "books.xml":
下面的代码片断将获取“books.xml”文件中的最后一个子节点:

//check if the last node is an element node
function get_lastchild(n) { var x=n.lastChild; while (x.nodeType!=1) { x=x.previousSibling; } return x; }
xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.documentElement;
var lastNode=get_lastchild(x);
for (var i=0;i<lastNode.childNodes.length;i++) { if (lastNode.childNodes[i].nodeType==1) { //Process only element nodes document.write(lastNode.childNodes[i].nodeName); document.write(" = "); document.write(lastNode.childNodes[i].childNodes[0].nodeValue); document.write("<br />"); } }

The output of the code above will be:
上述代码将输出下面的结果:

title = Learning XML
author = Erik T. Ray
year = 2003
price = 39.95


Try-It-Yourself Demos
自我演示

lastChild - Get the last child node in an XML document
lastChild - 获取XML文档中的最后一个子节点

评论 (0) All

登陆 | 还没注册?