aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_scope/index.html
blob: 0eb40990b3cd64c0bacd7883c267c996f4c19f65 (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
---
title: ':scope'
slug: 'Web/CSS/:scope'
tags:
  - ':scope'
  - CSS
  - Layout
  - Pseudo-class
  - Reference
  - Scoped Elements
  - Web
translation_of: 'Web/CSS/:scope'
---
<p>{{CSSRef}}</p>

<p><strong><code>:scope</code></strong> 属于 <a href="/zh-CN/docs/Web/CSS">CSS</a> <a href="/zh-CN/docs/Web/CSS/Pseudo-classes">伪类</a>,它表示作为选择器要匹配的参考点的元素。</p>

<pre class="brush: css">/* Selects a scoped element */
: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> 匹配你调用API的元素。</p>

<h2 id="用法">用法</h2>

{{csssyntax}}

<h2 id="示例">示例</h2>

<h3 id="Identity_match">Identity match</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 = "Yep, the element is its own scope as expected!";
}</pre>

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

<pre class="brush: html">&lt;p id="para"&gt;
  This is a paragraph. It is not an interesting paragraph. Sorry about that.
&lt;/p&gt;
&lt;p id="output"&gt;&lt;/p&gt;</pre>

<h4 id="结果">结果</h4>

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

<h3 id="Direct_children">Direct children</h3>

<p>当需要获取已检索到的的直接后代元素时,<code>:scope</code> 伪类很有用。</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="结果_2">结果</h4>

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

<h2 id="规范">规范</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>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<div class="hidden">该浏览器兼容性表格由结构化数据生成。如果你愿意做出贡献,可以访问 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 和提交PR。</div>

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

<h2 id="参考">参考</h2>

<ul>
 <li>{{cssxref(":root")}}{{cssxref("Pseudo-classes")}}</li>
 <li><a href="/zh-CN/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>