aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/_colon_in-range/index.html
blob: 45bb8e48879e103c62ba467bbc42aa115e6fed4b (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
---
title: ':in-range'
slug: 'Web/CSS/:in-range'
translation_of: 'Web/CSS/:in-range'
---
<div>{{CSSRef}}</div>

<div><strong><code>in-range</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> <a href="/en-US/docs/CSS/Pseudo-classes" title="Pseudo-classes">伪类</a> 代表一个 {{htmlelement("input")}} 元素,其当前值处于属性{{htmlattrxref("min", "input")}}{{htmlattrxref("max","input")}} 限定的范围之内.</div>

<pre class="brush: css no-line-numbers">/* 该伪类可选定任意的&lt;input&gt;, 但只有在该元素指定了取值范围,并且元素值处于指定范围时才有效*/
input:in-range {
  background-color: rgba(0, 255, 0, 0.25);
}</pre>

<p>该伪类用于给用户一个可视化的提示,表示输入域的当前值处于允许范围内。</p>

<div class="note">注意:该伪类仅适用于那些拥有(或可以接受)取值范围设定的元素。若缺少此类设定,元素值就无所谓“in-range”和“out-range”。</div>

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

{{csssyntax}}

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

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

<pre class="brush: html">&lt;form action="" id="form1"&gt;
  &lt;ul&gt;Values between 1 and 10 are valid.
    &lt;li&gt;
      &lt;input id="value1" name="value1" type="number" placeholder="1 to 10" min="1" max="10" value="12"&gt;
      &lt;label for="value1"&gt;Your value is &lt;/label&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
&lt;/form&gt;</pre>

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

<pre class="brush: css;">li {
  list-style: none;
  margin-bottom: 1em;
}

input {
  border: 1px solid black;
}

input:in-range {
  background-color: rgba(0, 255, 0, 0.25);
}

input:out-of-range {
  background-color: rgba(255, 0, 0, 0.25);
  border: 2px solid red;
}

input:in-range + label::after {
  content: 'okay.';
}

input:out-of-range + label::after {
  content: 'out of range!';
}</pre>

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

<div>{{EmbedLiveSample('Example', 600, 140)}}</div>

<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('HTML WHATWG', 'scripting.html#selector-in-range', ':in-range')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>Defines when <code>:in-range</code> matches elements in HTML.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS4 Selectors', '#in-range-pseudo', ':in-range')}}</td>
   <td>{{Spec2('CSS4 Selectors')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

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

<div>
<div>


<p>{{Compat("css.selectors.in-range")}}</p>
</div>
</div>

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

<ul>
 <li>{{cssxref(":out-of-range")}}</li>
 <li><a href="/en-US/docs/Learn/HTML/Forms/Form_validation">Form data validation</a></li>
</ul>