XML DOM μέθοδος compareDocumentPosition()
Ορισμός και χρήση
compareDocumentPosition()
Παράγεται η σύγκριση της θέσης δύο κόμβων στο δομικό επίπεδο (έγγραφο) της μεθόδου.
Syntax
nodeObject.compareDocumentPosition(node)
Parameters
Parameters | Description |
---|---|
node | Required. Node object. Specifies the node to be compared with the current node. |
Technical Details
DOM Version: | Core Level 3 Node Object |
---|---|
Return value: |
Numbers representing the relative positions of the two nodes. Possible return values include:
Note:The return value can also be a combination of values. For example, a return value of 20 indicates that the specified node is contained within the current node (16) and follows the current node (4). |
Example
The following code loads "books.xml" into xmlDoc and compares the positions of two nodes (the first and third <book> elements) in the DOM hierarchy:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName('book')[0]; var y = xmlDoc.getElementsByTagName('book')[2]; document.getElementById("demo").innerHTML = x.compareDocumentPosition(y); }
Most browsers treat whitespace or new lines as text nodes, but not Internet Explorer 9 and earlier versions. Therefore, in the above example, most browsers will output 4, while Internet Explorer 9 and earlier versions will output 2.
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
所有主流浏览器都支持 compareDocumentPosition() 方法。
注释:Internet Explorer 9 及更早版本不支持此方法。