blob: c2127a8aed5a38f75fb7cbbcee2ad7b99e45727b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
---
title: Node.isEqualNode
slug: Web/API/Node/isEqualNode
tags:
- API
- DOM
- Node
- 参考
- 方法
- 节点
translation_of: Web/API/Node/isEqualNode
---
<div>{{ ApiRef("DOM") }}</div>
<p> <code><strong>Node.isEqualNode() </strong></code>方法可以判断两个节点是否相等。当两个节点的类型相同,定义特征(defining characteristics)相同(对元素来说,即 id,孩子节点的数量等等),属性一致等,这两个节点就是相等的。一些具体的数据指出:多数时候的比较是根据节点的类型来的。</p>
<h3 id="语法">语法</h3>
<pre class="syntaxbox">var <em>isEqualNode</em> = <em>node</em>.isEqualNode(<var>otherNode</var>);
</pre>
<ul>
<li><font face="Consolas, Liberation Mono, Courier, monospace">otherNode: </font>比较是否相等的节点.</li>
</ul>
<h2 id="示例">示例</h2>
<p>在本例中,我们创建了三个 {{HTMLElement("div")}} 块。第一个和第三个 div 都拥有相同的内容和属性,第二个则不一样。然后我们运行 JavaScript ,使用 <code>isEqualNode()</code> 来比较这几个节点。</p>
<h3 id="HTML">HTML</h3>
<pre class="brush: html"><div>This is the first element.</div>
<div>This is the second element.</div>
<div>This is the first element.</div>
<p id="output"></p></pre>
<h3 id="JavaScript">JavaScript</h3>
<pre class="brush: js">let output = document.getElementById("output");
let divList = document.getElementsByTagName("div");
output.innerHTML += "div 0 equals div 0: " + divList[0].isEqualNode(divList[0]) + "<br/>";
output.innerHTML += "div 0 equals div 1: " + divList[0].isEqualNode(divList[1]) + "<br/>";
output.innerHTML += "div 0 equals div 2: " + divList[0].isEqualNode(divList[2]) + "<br/>";</pre>
<h3 id="结果">结果</h3>
<p>{{ EmbedLiveSample('示例', 480) }}</p>
<h3 id="规范">规范</h3>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('DOM WHATWG', '#dom-node-isequalnode', 'Node.isEqualNode')}}</td>
<td>{{Spec2('DOM WHATWG')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器支持">浏览器支持</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoDesktop("1.9")}}</td>
<td>9.0</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="相关链接">相关链接</h2>
<ul>
<li>{{domxref("Node.isSameNode()")}}</li>
</ul>
|