How may I check if 2 Dom element are same.
Form example
var element1 = document.getEl开发者_运维百科ementById("abc");
var element2 = document.getElementById("abc");
Now how should I chekc that these 2 elements are equal?
Thanks
element1
and element2
are references to the same place in the DOM tree. Just check
if( element1 == element2 )
{
alert("same") ;
}
Check out the API for DOM nodes. It seems like you could use isEqualNode
to achieve this.
https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode
const equal = element1.isEqualNode(element2) // true
A possibility would be to use .outerHTML . It will generate the HTML of the element, including itself (not only the contents like .innerHTML).
UPDATE: This answer (the text below) was wrong despite being the accepted answer. Instead of comparing the IDs use an equality check of the elements: element1 === element2
If the Ids are the same, they cannot be different. If the Ids are different they cannot be the same. So if you have both Ids you can just compare the two Ids.
精彩评论