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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
---
title: ':any'
slug: 'Web/CSS/:any'
tags:
- CSS
- 伪类选择器
- 实验性
translation_of: 'Web/CSS/:is'
translation_of_original: 'Web/CSS/:any'
---
<div>{{CSSRef}}{{SeeCompatTable}}</div>
<h2 id="Summary" name="Summary">概要</h2>
<p>The <code>:any()</code> <a href="/en-US/docs/CSS/Pseudo-classes" title="CSS/Pseudo-classes">pseudo-class</a> lets you quickly construct sets of similar selectors by establishing groups from which any of the included items will match. This is an alternative to having to repeat the entire selector for the one item that varies.</p>
<p>译文:any()伪类可以让您快速构建类似的选择器集合,通过建立包含所有包含项的组来匹配。这是另一种选择,它必须重复整个选择器,因为其中一个项目是不同的。</p>
<div class="note"><strong>Note : </strong>This pseudo-class is in progress to be standardized in <a class="external" href="http://dev.w3.org/csswg/selectors4/#matches" title="http://dev.w3.org/csswg/selectors4/#matches"><em>CSS Selectors Level 4</em></a> under the name <code>:matches()</code>. It is likely that the syntax and name of <code>:-<em>vendor</em>-any()</code> will be changed to reflect it in the near future.</div>
<div class="note">注意:这个伪类正在CSS选择器第4级按照名称:matches()进行标准化。很可能的语法和名称:- vendor-any()将在不久的将来被更改以反映它。</div>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre class="syntaxbox">{{csssyntax}}</pre>
<h3 id="Values" name="Values">值</h3>
<dl>
<dt><code>selector</code></dt>
<dd>选择器。这可以是由 <a class="external" href="http://www.w3.org/TR/css3-selectors/#simple-selectors" title="http://www.w3.org/TR/css3-selectors/#simple-selectors">CSS 3 简单选择器</a> 组成的简单选择器或多重选择器,并且可以包含后代组合器。</dd>
</dl>
<div class="note"><strong>注意:</strong>选择器可以<strong>不</strong>包含伪元素,并且仅允许的组合器是后代组合器。</div>
<h2 id="Examples" name="Examples">例子</h2>
<p>举个例子,如以下 CSS:</p>
<pre class="brush: css">/* 3 deep (or more) unordered lists use a square */
ol ol ul, ol ul ul, ol menu ul, ol dir ul,
ol ol menu, ol ul menu, ol menu menu, ol dir menu,
ol ol dir, ol ul dir, ol menu dir, ol dir dir,
ul ol ul, ul ul ul, ul menu ul, ul dir ul,
ul ol menu, ul ul menu, ul menu menu, ul dir menu,
ul ol dir, ul ul dir, ul menu dir, ul dir dir,
menu ol ul, menu ul ul, menu menu ul, menu dir ul,
menu ol menu, menu ul menu, menu menu menu, menu dir menu,
menu ol dir, menu ul dir, menu menu dir, menu dir dir,
dir ol ul, dir ul ul, dir menu ul, dir dir ul,
dir ol menu, dir ul menu, dir menu menu, dir dir menu,
dir ol dir, dir ul dir, dir menu dir, dir dir dir {
list-style-type: square;
}
</pre>
<p>可以写成这样:</p>
<pre class="brush: css">/* 3 deep (or more) unordered lists use a square */
:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) ul,
:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) menu,
:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) dir {
list-style-type: square;
}</pre>
<p>但是,不能写成这样(详见<a href="#Issues_with_performance_and_specificity">the section on performance</a>):</p>
<pre class="brush: css">:-moz-any(ol, ul, menu, dir) :-moz-any(ol, ul, menu, dir) :-moz-any(ul, menu, dir) {
list-style-type: square;
}</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>译文: 这个伪类选择器会在处理 HTML5 的 <a href="/en-US/docs/Sections_and_Outlines_of_an_HTML5_document" title="Sections and Outlines of an HTML5 document">sections and headings</a> 中尤其有用。因为 {{HTMLElement("section")}}, {{HTMLElement("article")}}, {{HTMLElement("aside")}}, 和 {{HTMLElement("nav")}} 可以被嵌套,如果不使用 <code>:any()</code> ,为了去匹配这些标签来给予样式会变得困难。</p>
<p>举个例子,当不使用 <code>:any()</code> ,给予不同的 DOM 结点层次深度下的所有的 {{HTMLElement("h1")}} 的元素样式时,将会变得特别的复杂。</p>
<pre class="brush: css">/* Level 0 */
h1 {
font-size: 30px;
}
/* Level 1 */
section h1, article h1, aside h1, nav h1 {
font-size: 25px;
}
/* Level 2 */
section section h1, section article h1, section aside h1, section nav h1,
article section h1, article article h1, article aside h1, article nav h1,
aside section h1, aside article h1, aside aside h1, aside nav h1,
nav section h1, nav article h1, nav aside h1, nav nav h1, {
font-size: 20px;
}
/* Level 3 */
/* ... don't even think about it*/
</pre>
<p>然而,通过使用 <code>:any()</code> ,这将变得很简单:</p>
<pre class="brush: css">/* Level 0 */
h1 {
font-size: 30px;
}
/* Level 1 */
:-moz-any(section, article, aside, nav) h1 {
font-size: 25px;
}
/* Level 2 */
:-moz-any(section, article, aside, nav)
:-moz-any(section, article, aside, nav) h1 {
font-size: 20px;
}
/* Level 3 */
:-moz-any(section, article, aside, nav)
:-moz-any(section, article, aside, nav)
:-moz-any(section, article, aside, nav) h1 {
font-size: 15px;
}</pre>
<h3 id="Issues_with_performance_and_specificity" name="Issues_with_performance_and_specificity">性能与特异性问题</h3>
<p><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=561154" title="https://bugzilla.mozilla.org/show_bug.cgi?id=561154">Bug 561154</a> 追踪了一个关于 Gecko 作为内核的浏览器在使用 <code>:-moz-any()</code> 不正确的问题。在最新的浏览器(从 Firefox 12) 实现 <code>:-moz-any()</code> 有一个通用的规则,在使用这个伪类选择器时,当使用它作为最右边的选择器会比使用 ID, Class, 标签选择器作为最右边的选择器要来的慢。</p>
<p>例如:</p>
<pre class="brush: css">.a > :-moz-any(.b, .c)
</pre>
<p>会比下面的表达式慢</p>
<pre class="brush: css">.a > .b, .a > .c
</pre>
<p>而下面的表达式将会是最快的</p>
<pre class="brush: css">:-moz-any(.a, .d) > .b, :-moz-any(.a, .d) > .c
</pre>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Firefox (Gecko)</th>
<th>Chrome</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatGeckoDesktop("2")}}{{property_prefix("-moz")}}</td>
<td>12.0 (534.30){{property_prefix("-webkit")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>5<br>
{{property_prefix("-webkit")}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}{{property_prefix("-webkit")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>5<br>
{{property_prefix("-webkit")}}</td>
</tr>
</tbody>
</table>
</div>
|