blob: 5865081c84339ae4ccba75cfe310b7cef07c9ab0 (
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
|
---
title: Node.compareDocumentPosition
slug: Web/API/Node/compareDocumentPosition
tags:
- DOM
- DOM Element Methods
- Gecko
- Gecko DOM Reference
translation_of: Web/API/Node/compareDocumentPosition
---
<div>{{ApiRef}}</div>
<h2 id="Summary" name="Summary">概要</h2>
<p>そのノードと別のノードの位置を比較し、結果となるビットマスクを返します。</p>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox"><var>node</var>.compareDocumentPosition( <var>otherNode</var> )
</pre>
<h3 id="パラメーター">パラメーター</h3>
<dl>
<dt><code>node</code></dt>
<dd>比較元ノード</dd>
<dt><code>otherNode</code></dt>
<dd><em>node</em> と比較する別ノード</dd>
</dl>
<h3 id="返り値">返り値</h3>
<p>呼び出し元の <code>node</code> と {{domxref("Document")}} 内の <code>otherNode</code> の関係を表すビットの整数値。複数の条件に適応する場合、1つ以上のビットがセットされることがあります。<code>compareDocumentPosition()</code> を呼び出した <code>node</code> と比較して、<code>otherNode</code> が 文書のより前にあり、かつ <code>node</code> を含んでいるならば、<code>DOCUMENT_POSITION_CONTAINS</code> と <code>DOCUMENT_POSITION_PRECEDING</code> のビットがセットされ、結果は0x0Aもしくは10進数の10になります。</p>
<h2 id="Notes" name="Notes">注記</h2>
<p>戻り値は以下の値を持つビットマスクの何れかとなります。</p>
<table>
<thead>
<tr>
<th scope="col">名称</th>
<th scope="col">値</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>DOCUMENT_POSITION_DISCONNECTED</code></td>
<td>1</td>
</tr>
<tr>
<td><code>DOCUMENT_POSITION_PRECEDING</code></td>
<td>2</td>
</tr>
<tr>
<td><code>DOCUMENT_POSITION_FOLLOWING</code></td>
<td>4</td>
</tr>
<tr>
<td><code>DOCUMENT_POSITION_CONTAINS</code></td>
<td>8</td>
</tr>
<tr>
<td><code>DOCUMENT_POSITION_CONTAINED_BY</code></td>
<td>16</td>
</tr>
<tr>
<td><code>DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC</code></td>
<td>32</td>
</tr>
</tbody>
</table>
<h2 id="Example" name="Example">例</h2>
<pre class="brush:js;highlight:[3]">var head = document.getElementsByTagName('head').item(0);
if ( head.compareDocumentPosition(document.body) & Node.DOCUMENT_POSITION_FOLLOWING ) {
console.log("head 要素は body 要素より前に記述されています。");
} else {
console.log("head 要素は body 要素の前に配置しなくてはなりません。");
}
</pre>
<div class="note">
<p><strong>注記:</strong> <code>compareDocumentPosition</code> の戻り値はビットマスクです。よって、有意な結果を得るには <a href="/ja/docs/JavaScript/Reference/Operators/Bitwise_Operators">ビット演算子</a>の "<code>&</code>" を用いなくてはならない点に注意して下さい。</p>
</div>
<h2 id="Specification" name="Specification">仕様</h2>
<table>
<thead>
<tr>
<th>仕様書</th>
<th>策定状況</th>
<th>コメント</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition">DOM Level 3</a></td>
<td>Recommendation</td>
<td></td>
</tr>
<tr>
<td><a href="http://dom.spec.whatwg.org/#dom-node-comparedocumentposition">DOM Standard</a></td>
<td>Living standard</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{domxref("Node.contains")}}</li>
<li><a href="http://ejohn.org/blog/comparing-document-position/">John Resig - Comparing Document Position</a></li>
</ul>
|