:indeterminate
:indeterminate CSS 伪类表示任何处于不确定状态的表单元素,例如通过 JavaScript 设置为 indeterminate 状态的复选框,作为组内成员但所有单选按钮都未选中的单选按钮,以及没有 value 属性的 <progress> 元素。
css
/* Selects any <input> whose state is indeterminate */
input:indeterminate {
background: lime;
}
此选择器定位的元素包括:
<input type="checkbox">元素,其indeterminate属性设置为true。<input type="radio">元素,具有相同的name值且所有都未checked。<progress>元素,没有value属性,使其处于不确定状态。
语法
css
:indeterminate {
/* ... */
}
示例
复选框和单选按钮
此示例为与不确定表单字段关联的标签应用特殊样式。
HTML
html
<fieldset>
<legend>Checkbox</legend>
<div>
<input type="checkbox" id="checkbox" />
<label for="checkbox">This checkbox label starts out lime.</label>
</div>
</fieldset>
<fieldset>
<legend>Radio</legend>
<div>
<input type="radio" id="radio1" name="radioButton" value="val1" />
<label for="radio1">First radio label starts out lime.</label>
</div>
<div>
<input type="radio" id="radio2" name="radioButton" value="val2" />
<label for="radio2">Second radio label also starts out lime.</label>
</div>
</fieldset>
CSS
css
input:indeterminate + label {
background: lime;
}
JavaScript
js
const inputs = document.getElementsByTagName("input");
for (const input of inputs) {
input.indeterminate = true;
}
结果
进度条
HTML
html
<progress></progress>
CSS
css
progress {
margin: 4px;
}
progress:indeterminate {
width: 80vw;
height: 20px;
}
结果
规范
| 规范 |
|---|
| HTML # 选择器-不确定 |
| 选择器 Level 4 # 不确定伪类 |
浏览器兼容性
加载中…
另见
- Web 表单 — 处理用户数据
- 为网页表单添加样式
<input type="checkbox">元素的indeterminate属性<input>及其实现的HTMLInputElement接口。:checkedCSS 选择器允许你根据复选框是否被选中来为其设置样式