XML DOM compareDocumentPosition () method
Definition and Usage
compareDocumentPosition () method to compare the current location of the document node according to a specified node in document order.
grammar
elementNode.compareDocumentPostition(node)
参数 | 描述 |
---|---|
node | 必选。规定与当前节点进行比较的节点。 |
Examples
The following code fragment uses loadXMLDoc () to " the Books.xml " into xmlDoc and compare the first and the third <book> node:
Examples
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0];
y=xmlDoc.getElementsByTagName('book')[2];
document.write(x.compareDocumentPosition(y));
x=xmlDoc.getElementsByTagName('book')[0];
y=xmlDoc.getElementsByTagName('book')[2];
document.write(x.compareDocumentPosition(y));
Output:
4
try it"
Firefox and most other browsers, the empty spaces between the nodes will generate or wrap as text nodes, while Internet Explorer will ignore whitespace text nodes between nodes generated. Thus, in the example above, Mozilla browser will output 4, and Internet Explorer will output 2.
For more information about browser differences, please visit us in our XML DOM tutorial DOM browser section.
Element object