SYNCER

SYNCER

Node.compareDocumentPosition() - 2つのノードの位置関係を確認する

公開日:

Node.compareDocumentPosition()は、Nodeのメソッドです。自身と対象のノードの位置関係を確認します。結果は、定数に対応する値のビットマスクで返ります。

概要

IDL

unsigned short compareDocumentPosition(Node other);

引数

第1引数: other

Node other

比較対象のNodeを指定する。

返り値

unsigned short

自身と相手の位置関係を表すビットマスク。位置関係は複数が混在する場合がある。

定数説明
0x01DOCUMENT_POSITION_DISCONNECTED2つのノードは同じツリーにない。
0x02DOCUMENT_POSITION_PRECEDING自分から見て、相手は前にある。
0x04DOCUMENT_POSITION_FOLLOWING自分から見て、相手は後にある。
0x08DOCUMENT_POSITION_CONTAINS自分から見て、相手は祖先である。
0x10DOCUMENT_POSITION_CONTAINED_BY自分から見て、相手は子孫である。
0x20DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC位置関係が定まってない。例えば、document.createTextNode()を比較した時など。

脚注

unsigned short

0〜216-1の整数。

仕様書

https://dom.spec.whatwg.org/#node

説明

compareDocumentPosition()は、自身と相手の位置関係を調べるためのメソッドです。位置関係は、複数の種類が混在する場合があります。例えば、下記の場合、div#fooから見て、p#barは、後にあり(0x04)、かつ、子孫です(0x10)。そういうことがあるため、結果はビットマスクで返ります。if文などで比較を行なうには、ビット演算子を利用しましょう。これは難しい話ではなく単純に、=====ではなく&を使えば、truefalseかを判定できるということです。

HTML

<!-- div#fooから見てp#barは後にある -->
<!-- div#fooから見てp#barは子孫である -->
<div id="foo">
	<p id="bar">SYNCER</p>
</div>

JavaScript

// 要素を取得
var element = document.getElementById( "foo" ) ;
var otherNode = document.getElementById( "bar" ) ;

// メソッドを実行
var result = element.compareDocumentPosition( otherNode ) ;

// ビット演算子で確認 (値を利用)
if ( result & 0x01 ) {}	// false
if ( result & 0x02 ) {}	// false
if ( result & 0x04 ) {}	// true
if ( result & 0x08 ) {}	// false
if ( result & 0x10 ) {}	// true
if ( result & 0x20 ) {}	// false

// ビット演算子で確認 (定数を利用)
if ( result & Node.DOCUMENT_POSITION_DISCONNECTED ) {}	// false
if ( result & Node.DOCUMENT_POSITION_PRECEDING ) {}	// false
if ( result & Node.DOCUMENT_POSITION_FOLLOWING ) {}	// true
if ( result & Node.DOCUMENT_POSITION_CONTAINS ) {}	// false
if ( result & Node.DOCUMENT_POSITION_CONTAINED_BY ) {}	// true
if ( result & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC ) {}	// false

サンプルコード

HTML

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>【デモ】Node.compareDocumentPosition() - 2つのノードの位置関係を確認する</title>
		<link rel="canonical" href="https://syncer.jp/Web/API_Interface/Reference/IDL/Node/compareDocumentPosition/">
		<meta content="width=device-width,initial-scale=1.0" name="viewport">
		<script src="https://demo.syncer.jp/js/demo-web_api_interface.js"></script>
		<script>
window.addEventListener( "load", function () {
	syncer.setup ( {
		method: {
			execute: {
				use: true ,
				callback: [
					{
						function: (typeof myMethod === "function" ? myMethod : null) ,
					}
				] ,
			} ,
		} ,
		error: {
			use: true ,
		} ,
		console: {
			use: true ,
		} ,
		htmlElement: {
			use: true ,
			elements: [ {
				element: document.getElementById( "hoge" ) ,
				content: true ,
			}, ] ,
		} ,
	} ) ;
} ) ;
		</script>
		<style>
			body {
				font: 400 15px/1.618 -apple-system, BlinkMacSystemFont, Segoe UI, Helvetica Neue, Hiragino Kaku Gothic ProN, メイリオ, meiryo, sans-serif ;
				background-color: #fff ;
			}
		</style>
	</head>
	<body>

<p>下記のdiv要素(自分)とp要素(相手)の位置関係を、compareDocumentPosition()で取得します。デフォルトでは、DOCUMENT_POSITION_FOLLOWING(相手が自分の後方)とDOCUMENT_POSITION_CONTAINED_BY(相手が自分の子孫)を満たしています。</p>

<div id="hoge">
	<p id="fuga">SYNCER</p>
</div>

<script>
// compareDocumentPosition()
function myMethod () {
	// div要素を取得
	var element1 = document.getElementById( "hoge" ) ;

	// p要素を取得
	var element2 = document.getElementById( "fuga" ) ;

	// メソッドを実行
	var returnValue = element1.compareDocumentPosition( element2 ) ;

	// 返り値を確認
	console.log( "返り値", returnValue ) ;

	// 位置関係をビット演算子で比較
	console.log( "returnValue & DOCUMENT_POSITION_DISCONNECTED", (returnValue & Node.DOCUMENT_POSITION_DISCONNECTED) ) ;
	console.log( "returnValue & DOCUMENT_POSITION_PRECEDING", (returnValue & Node.DOCUMENT_POSITION_PRECEDING) ) ;
	console.log( "returnValue & DOCUMENT_POSITION_FOLLOWING", (returnValue & Node.DOCUMENT_POSITION_FOLLOWING) ) ;
	console.log( "returnValue & DOCUMENT_POSITION_CONTAINS", (returnValue & Node.DOCUMENT_POSITION_CONTAINS) ) ;
	console.log( "returnValue & DOCUMENT_POSITION_CONTAINED_BY", (returnValue & Node.DOCUMENT_POSITION_CONTAINED_BY) ) ;
	console.log( "returnValue & DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC", (returnValue & Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) ) ;
}
</script>

	</body>
</html>

デモを開く

デモ

</head><body>までを含めて下さい。

<body> <div id="___body">

</div> <script>...</script> </body> </html>

<style>

</style>

<script>

</script>

サポート状況

Node.compareDocumentPosition()のサポート状況です。

ブラウザサポート状況ブラウザサポート状況
ChromeSupportedFirefoxSupported
EdgeSupportedInternet ExplorerSupported 9+
SafariSupportedOperaSupported
iOS SafariSupportedAndroidSupported

Chrome

バージョンサポート状況公開時期シェア
57Supported2017年3月頃0.1%
56Supported2017年1月頃12.02%
55Supported2016年12月頃12.36%
54Supported2016年10月頃0.44%
53Supported2016年9月頃0.25%
52Supported2016年7月頃0.27%
51Supported2016年6月頃0.4%
50Supported2016年4月頃0.32%
49Supported2016年3月頃0.47%
48Supported2016年1月頃0.02%
47Supported2015年12月頃0.06%
46Supported2015年10月頃0.56%
45Supported2015年9月頃0.01%
44Supported2015年7月頃0.83%
43Supported2015年5月頃0.05%
42Supported2015年4月頃0.05%
41Supported2015年3月頃0.01%
40Supported2015年1月頃0.59%
39Supported2014年11月頃0.02%
38Supported2014年10月頃0.08%
37Supported2014年8月頃0.01%
36Supported2014年7月頃0.01%
35Supported2014年5月頃0.01%
34Supported2014年4月頃0.14%
33Supported2014年2月頃0.01%
32Supported2014年1月頃0%
31Supported2013年11月頃0.01%
30Supported2013年10月頃0.03%
29Supported2013年8月頃0%
28Supported2013年6月頃0.03%
27Supported2013年5月頃0.01%
26Supported2013年3月頃0%
25Supported2013年2月頃0%
24Supported2013年1月頃0%
23Supported2012年11月頃0%
22Supported2012年9月頃0%
21Supported2012年7月頃0%
20Supported2012年6月頃0%
19Supported2012年5月頃0%
18Supported2012年3月頃0.01%
17Supported2012年2月頃0.01%
16Supported2011年12月頃0%
15Supported2011年10月頃0%
14Supported2011年9月頃0%

Firefox

バージョンサポート状況公開時期シェア
52Supported2017年3月頃0.12%
51Supported2017年1月頃1.7%
50Supported2016年11月頃0.78%
49Supported2016年9月頃0.02%
48Supported2016年8月頃0.07%
47Supported2016年6月頃0.03%
46Supported2016年4月頃0.01%
45Supported2016年3月頃0.1%
44Supported2016年1月頃0.01%
43Supported2015年12月頃0.02%
42Supported2015年11月頃0.01%
41Supported2015年9月頃0%
40Supported2015年8月頃0.02%
39Supported2015年7月頃0.01%
38Supported2015年5月頃0.02%
37Supported2015年3月頃0%
36Supported2015年2月頃0.01%
35Supported2015年1月頃0%
34Supported2014年12月頃0.01%
33Supported2014年10月頃0%
32Supported2014年9月頃0%
31Supported2014年7月頃0.01%
30Supported2014年6月頃0%
29Supported2014年4月頃0%
28Supported2014年3月頃0.01%
27Supported2014年2月頃0%
26Supported2013年12月頃0%
25Supported2013年10月頃0%
24Supported2013年9月頃0%
23Supported2013年8月頃0%
22Supported2013年6月頃0%
21Supported2013年5月頃0%
20Supported2013年4月頃0%
19Supported2013年2月頃0%
18Supported2013年1月頃0%
17Supported2012年11月頃0%
16Supported2012年10月頃0%
15Supported2012年8月頃0%
14Supported2012年7月頃0%
13Supported2012年6月頃0%
12Supported2012年4月頃0%
11Supported2012年3月頃0%
10Supported2012年1月頃0%
9Supported2011年12月頃0%
8Supported2011年11月頃0%
7Supported2011年9月頃0%
6Supported2011年8月頃0%
5Supported2011年6月頃0%
4Supported2011年3月頃0%

Edge

バージョンサポート状況公開時期シェア
14Supported2016年2月頃1.05%
13Supported2015年9月頃0.08%

Internet Explorer

バージョンサポート状況公開時期シェア
11Supported2013年10月頃4.99%
10Supported2012年8月頃0.09%
9Supported2011年3月頃0.18%
8Not Supported2009年3月頃0.04%
7Not Supported2006年10月頃0.01%
6Not Supported2001年8月頃0%

Safari

バージョンサポート状況公開時期シェア
10Supported2016年10月頃0%
9.1Supported2015年9月頃0%
8.0Supported2014年10月頃0%
7.1Supported2013年10月頃0%
6.0Supported2012年7月頃0%
5.1Supported2011年7月頃0%
4Supported2009年6月頃0%

Opera

バージョンサポート状況公開時期シェア
43Supported2017年2月頃0.06%
42Supported2016年12月頃0.09%
41Supported2016年10月頃0.01%
40Supported2016年9月頃0%
39Supported2016年8月頃0%
38Supported2016年6月頃0%
37Supported2016年5月頃0.01%
36Supported2016年3月頃0.01%
35Supported2016年2月頃0%
34Supported2015年12月頃0%
33Supported2015年10月頃0%
32Supported2015年9月頃0%
31Supported2015年8月頃0%
30Supported2015年6月頃0%
29Supported2015年4月頃0%
28Supported2015年3月頃0%
27Supported2015年1月頃0%
26Supported2014年12月頃0%
25Supported2014年10月頃0%
24Supported2014年9月頃0%
23Supported2014年7月頃0%
22Supported2014年6月頃0%
21Supported2014年5月頃0.01%
20Supported2014年3月頃0%
19Supported2014年1月頃0%
18Supported2013年11月頃0%
17Supported2013年10月頃0%
16Supported2013年8月頃0%
15Supported2013年7月頃0%
12Supported2012年6月頃0.02%
11.6Supported2011年12月頃0%
11.1Supported2011年4月頃0%

iOS Safari

バージョンサポート状況公開時期シェア
10.0Supported2016年9月頃39.65%
9.1Supported2015年9月頃5.39%
8.3Supported2014年9月頃1.03%
7.0Supported2013年9月頃0.24%
6.0Supported2012年9月頃0.05%
5.1Supported2011年10月頃0.03%

Android

バージョンサポート状況公開時期シェア
4.4Supported2013年10月頃0%
4.3Supported2013年7月頃0%
4.2Supported2013年3月頃0.03%
4.1Supported2012年12月頃0%
4.0Supported2012年6月頃1.61%
2.3Supported2011年10月頃0%