aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_focus/index.html
blob: b35aa2b79a747c585c4e5a66acf8f788e604ccd6 (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
---
title: ':focus'
slug: 'Web/CSS/:focus'
tags:
  - CSS
  - Layout
  - Reference
  - Web
  - 伪类
translation_of: 'Web/CSS/:focus'
---
<p>{{CSSRef}}</p>

<p>CSS<a href="/en/CSS/Pseudo-classes" title="Pseudo-classes">伪类</a> <code>:focus</code>表示获得焦点的元素(如表单输入)。当用户点击或触摸元素或通过键盘的 “tab” 键选择它时会被触发。</p>

<pre class="brush: css">/* Selects any &lt;input&gt; when focused */
input:focus {
  color: red;
}</pre>

<div class="note">
<p><strong>提示:</strong>此伪类仅适用于焦点元素本身。如果要选择包含焦点元素的元素,请使用{{cssxref(":focus-within")}}</p>
</div>

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

{{csssyntax}}

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

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;input class="red-input" value="I'll be red when focused."&gt;&lt;br&gt;
&lt;input class="blue-input" value="I'll be blue when focused."&gt;</pre>

<h3 id="CSS">CSS</h3>

<pre class="brush: css">.red-input:focus {
  background: yellow;
  color: red;
}

.blue-input:focus {
  background: yellow;
  color: blue;
}</pre>

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

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

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'scripting.html#selector-focus', ':focus')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Defines HTML-specific semantics.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#focus-pseudo', ':focus')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Selectors', '#the-user-action-pseudo-classes-hover-act', ':focus')}}</td>
   <td>{{Spec2('CSS3 Selectors')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'selector.html#dynamic-pseudo-classes', ':focus')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

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

<div id="compat-mobile"> </div>

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

<ul>
 <li>{{cssxref(":focus-within")}}</li>
</ul>