Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第 71 期(JavaScript-DOM):复选框的半选 #74

Open
wingmeng opened this issue Jul 27, 2019 · 0 comments
Open

第 71 期(JavaScript-DOM):复选框的半选 #74

wingmeng opened this issue Jul 27, 2019 · 0 comments

Comments

@wingmeng
Copy link
Collaborator

原生的表单控件 checkbox 有两种状态值:选中(checked)和未选中(unchecked)。其实在视觉效果上,checkbox 有三种状态:checkeduncheckedindeterminate。其中 indeterminate 表示不确定或半选状态,看起来就像这样子:

image

与前两个状态值不同,indeterminate 状态无法在 HTML 中设置(HTML 中压根儿就没有 indeterminate 这个属性),你只能通过 JavaScript 来设置它。

checkboxElement.indeterminate = true;  // 设置 checkboxElement 为半选状态
checkboxElement.indeterminate;  // 判断 checkboxElement 是否为半选状态

注:checkbox 的 indeterminate 状态仅仅是视觉上的,它的真实值仍然只有 checkedunchecked,这意味着这个状态的真正价值只是在用户界面上看起来更友好而已。

另外 CSS 中有 :indeterminate 这个伪类:

/* 半选时让紧邻的 label 标签背景色变为红色 */
input:indeterminate + label {
  background: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant