Check if two nodes are, in fact, the same node:
var item1 = document.getElementById("myList1"); // An <ul> element with id="myList"
var item2 = document.getElementsByTagName("UL")[0]; // The first <ul> element in the document
var x = item1.isSameNode(item2);
Try it Yourself »
More "Try it Yourself" examples below.
The isSameNode() method checks if two nodes are the same node.
The isSameNode() method returns true if the two nodes are the same node, otherwise false.
Tip: Use the isEqualNode() method to check if two nodes are equal, but not necessarily the same node.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
isSameNode() | Yes | 9.0 | Not supported | Yes | Yes |
Note: Firefox stopped supporting this method as of version 10, because the method has been deprecated in the DOM version 4. Instead, you should use === to compare if two nodes are the same (See "More Examples" below).
node.isSameNode(
node)
Parameter | Type | Description |
---|---|---|
node | Node object | Required. The node you want to compare the specified node with |
Return Value: | A Boolean, returns true if the two nodes are the same node, otherwise false |
---|---|
DOM Version | Core Level 3 Node Object |
Using the === operator to check if two nodes are the same node:
var item1 = document.getElementById("myList");
var item2 = document.getElementsByTagName("UL")[0];
if (item1 === item2) {
alert("THEY ARE THE SAME!!");
} else {
alert("They are not the same.");
}
Try it Yourself »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!