当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > nextSibling 属性
The nextSibling property returns the next sibling node (the following node in the same tree level) of the selected element
nextSilbing属性的作用是:返回已选元素的下一个同属节点(同级节点中的下一个)。
If there is no such node, this property returns null.
如果不包含这类节点,该属性将返回空值。
elementNode.nextSibling |
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 next sibling node.
注意:IE将跳过在节点之间产生的空格文档节点(如:换行字符),而Mozilla不会这样。在下面的案例中,我们将书写一个用于检测下一个同属节点的节点类型的函数。
Element nodes has a nodeType of 1, so if the next sibling node is not an element node, it moves to the next node, and checks if this node is an element node. This continues until the next sibling 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().
在所有案例中,我们将使用“books.xml”文件以及JavaScript 函数“loadXMLDoc()”。
The following code fragment gets the next sibling node from the first <title> element in the XML document:
下面的代码片断将获取XML文档中的第一个<title>元素的下一个同属节点:
//check if the next sibling node is an element node function get_nextsibling(n) { var x=n.nextSibling; while (x.nodeType!=1) { x=x.nextSibling; } return x; } xmlDoc=loadXMLDoc("books.xml"); var x=xmlDoc.getElementsByTagName("title")[0]; document.write(x.nodeName); document.write(" = "); document.write(x.childNodes[0].nodeValue); var y=get_nextsibling(x); document.write("<br />Next sibling: "); document.write(y.nodeName); document.write(" = "); document.write(y.childNodes[0].nodeValue); |
The output of the code above will be:
上述代码将输出下面的结果:
title = Everyday Italian Next sibling: author = Giada De Laurentiis |
nextSibling - Get the next sibling of a node
nextSibling - 获取下一个同属节点
previousSibling - Get the previous sibling of a node
previousSibling - 获取上一个同属节点