: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 選擇器允許你根據複選框是否被選中來為其設定樣式