aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/css/_colon_scope/index.md
blob: 0dbe004e783d03d232a11199755fb6c174f7c257 (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
---
title: ':scope'
slug: 'Web/CSS/:scope'
tags:
  - CSS
  - Layout
  - Reference
  - Web
  - スコープ付き要素
  - セレクター
  - 疑似クラス
translation_of: 'Web/CSS/:scope'
---
<div>{{CSSRef}}</div>

<p><strong><code>:scope</code></strong> は <a href="/ja/docs/Web/CSS">CSS</a> の<a href="/ja/docs/Web/CSS/Pseudo-classes">疑似クラス</a>で、セレクターが選択する対象の参照点である要素を表します。</p>

<pre class="brush: css">/* スコープとなる要素を選択 */
:scope {
  background-color: lime;
}</pre>

<p>現在、スタイルシートで使用したときは、現時点ではスコープ付きの要素を明確に確立する方法がないため、 <code>:scope</code> は {{cssxref(":root")}} と同じです。 DOM API ({{domxref("Element.querySelector", "querySelector()")}}, {{domxref("Element.querySelectorAll", "querySelectorAll()")}}, {{domxref("Element.matches", "matches()")}}, {{domxref("Element.closest()")}} など) で使用したときは、 <code>:scope</code> はメソッドを呼び出した要素を選択します。</p>

<h2 id="Syntax" name="Syntax">構文</h2>

{{csssyntax}}

<h2 id="Example" name="Example">例</h2>

<h3 id="Identity_match" name="Identity_match">ID の一致</h3>

<p>この例では、 {{domxref("Element.matches()")}} メソッドから <code>:scope</code> を使用して呼び出される要素を選択する方法を紹介します。</p>

<h4 id="JavaScript">JavaScript</h4>

<pre class="brush: js">let paragraph = document.getElementById("para");
let output = document.getElementById("output");

if (paragraph.matches(":scope")) {
  output.innerText = "はい、この要素は自分自身のスコープ内にあります!";
}</pre>

<h4 id="HTML">HTML</h4>

<pre class="brush: html">&lt;p id="para"&gt;
  これは段落です。面白い段落ではありません。すみません。
&lt;/p&gt;
&lt;p id="output"&gt;&lt;/p&gt;</pre>

<h4 id="Result" name="Result">結果</h4>

<div>{{ EmbedLiveSample('Identity_match') }}</div>

<h3 id="Direct_children" name="Direct_children">直接の子</h3>

<p><code>:scope</code> 疑似クラスが有用だと示されるのは、すでに受け取っている {{domxref("Element")}} の直接の子を取得する必要がある場合です。</p>

<h4 id="JavaScript_2">JavaScript</h4>

<pre class="brush: js">var context = document.getElementById('context');
var selected = context.querySelectorAll(':scope &gt; div');

document.getElementById('results').innerHTML = Array.prototype.map.call(selected, function (element) {
    return '#' + element.getAttribute('id');
}).join(', ');</pre>

<h4 id="HTML_2">HTML</h4>

<pre class="brush: html">&lt;div id="context"&gt;
    &lt;div id="element-1"&gt;
        &lt;div id="element-1.1"&gt;&lt;/div&gt;
        &lt;div id="element-1.2"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;div id="element-2"&gt;
        &lt;div id="element-2.1"&gt;&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
    Selected elements ids :
    &lt;span id="results"&gt;&lt;/span&gt;
&lt;/p&gt;</pre>

<h4 id="Result_2" name="Result_2">結果</h4>

<p>{{ EmbedLiveSample('Direct_children') }}</p>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
   <th scope="col">状態</th>
   <th scope="col">備考</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{ SpecName('CSS4 Selectors', '#the-scope-pseudo', ':scope') }}</td>
   <td>{{ Spec2('CSS4 Selectors') }}</td>
   <td>初回定義</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat("css.selectors.scope")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{cssxref(":root")}} <a href="/ja/docs/Web/CSS/Pseudo-classes">疑似クラス</a></li>
 <li><a href="/ja/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors">セレクターを使った DOM の配置</a></li>
 <li>{{domxref("Element.querySelector()")}} 及び {{domxref("Element.querySelectorAll()")}}</li>
 <li>{{domxref("Document.querySelector()")}} 及び {{domxref("Document.querySelectorAll()")}}</li>
 <li>{{domxref("DocumentFragment.querySelector()")}} 及び {{domxref("DocumentFragment.querySelectorAll()")}}</li>
 <li>{{domxref("ParentNode.querySelector()")}} 及び {{domxref("ParentNode.querySelectorAll()")}}</li>
</ul>