当前位置: 首页 > 网络学院 > XML相关教程 > XML DOM > compareDocumentPosition() 方法

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 中的 compareDocumentPosition() 方法


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

Definition and Usage
定义和用法

The compareDocumentPosition() method compares the document position of the current node, with a specified node, according to the document order.
compareDocumentPosition()方法的作用是:按照节点所在文档中的顺序比较当前节点和指定节点的位置。

Syntax
语法

nodeObject.compareDocumentPostition(node)

Parameter
参数
Description
描述
node Required. Specifies the node to compare with the current node
必要参数。指定需要和当前节点进行比较的节点对象
 

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

Example
案例

The following code fragment compares the document position of the first and the third <book> element:
下面的代码片断将比较“books.xml”文件中第一个节点和第三个<book>节点:

xmlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book')[0];
var y=xmlDoc.getElementsByTagName('book')[2];
document.write(x.compareDocumentPosition(y));

Output:
输出结果:

4

Internet Explorer will skip the white-space text nodes that are generated between nodes (e.g. new line characters), while Mozilla will not. So, in the example above, Mozilla browsers will output 4, while Internet Explorer will output 2.
IE将跳过在节点之间产生的空格文档节点(如:换行字符),而Mozilla不会这样。在下面的案例中,Mozilla浏览器将输出4,而IE将输出2。

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


Try-It-Yourself Demos
自我演示

compareDocumentPosition() - Compare the document position of two nodes
compareDocumentPosition() - 比较两个节点所在文档的位置

评论 (0) All

登陆 | 还没注册?