HTML DOM อิเล็กทรอนิก วิธี isSameNode()

Definition and usage

isSameNode() The method checks if two nodes are the same node.

isSameNode() The method returns true,If two nodes are the same node, otherwise returns false.

Hint:Use isEqualNode() method To check if two nodes are equal, but not necessarily the same node.

Example

Example 1

Check if two nodes are actually 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

Example 2

Check if two nodes are the same node using the === operator:

var item1 = document.getElementById("myList");
var item2 = document.getElementsByTagName("UL")[0];
if (item1 === item2) {
  alert("THEY ARE THE SAME!!");
}
  alert("They are not the same.");
}

Try it yourself

Syntax

node.isSameNode(node)

Parameter

Parameter Type Description
node Node object Required. The node to be compared with the specified node.

Technical details

Return value: boolean value, returns true if two nodes are the same node, otherwise returns false.
DOM version: Core Level 3 Node Object

การสนับสนุนบราวเซอร์

ตัวเลขในตารางบอกเล่าถึงรุ่นบราวเซอร์ที่สนับสนุนวิธีนี้ครบถ้วน

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
เข้าถึง 9.0 ไม่เข้าถึง เข้าถึง เข้าถึง

ทุกบราวเซอร์ที่เป็นที่นิยมเข้าถึง isSameNode() วิธีนี้ ยกเว้น Firefox

หมายเหตุ:Firefox รุ่น 10 หยุดสนับสนุนวิธีนี้ เพราะวิธีนี้ถูกทำเลิกใน DOM version 4 คุณควรใช้ === เพื่อเปรียบเทียบจุดหนึ่งกับจุดหนึ่ง

หมายเหตุ:Internet Explorer 8 และเวอร์ชั่นก่อนหน้านี้ไม่สนับสนุนวิธีนี้