intro
布尔属性,常见于表单的控件。
有checked, selected, required, disabled, enabled
等等。
HTML 规范取值
- 不出现在元素的开始标签。视为
false
。
<option>
- 出现在元素的开始标签。出现皆视为
true
。- 取值为属性的名称字符串。
<option selected="selected">
- 最小化形式(属性名单独出现即可)。
<option selected>
推荐写法。
- 取值为属性的名称字符串。
关于boolean attribute
的取值问题,其实赋值为空串""
,数值1, 0, 100等
,其他字符串"false", "true"
等,
都会自动转换为true。但还是推荐统一写法。
JS及jQuery的读写操作
- 读取
boolean flag = e.selected
boolean flag = $(e).prop("selected")
boolean flag = $(e).is(":selected")
- 修改
$(e).attr("selected", false)
jQuery 1.6之前,可以使用attr()
操作。
$(e).prop("selected", false)
jQuery 1.6开始,推荐使用prop()
操作。
boolean attribute
Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element).
Their appearance in the start tag of an element implies that the value of the attribute is "true".
Their absence implies a value of "false".
Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").
一些attribute
扮演着boolean变量的角色(比如说OPTION元素的selected attribute
)。
他们出现在元素的开始标签中,意味着attribute
的值是true
,不出现则意味着false
值。
boolean attribute
可以合法地接受一个值:这个attribute
本身的名字(如:selected="selected"
)。
这个例子定义selected attribute
成为一个boolean attribute
。
selected (selected) #IMPLIED -- option is pre-selected --
这个`attribute`出现在元素的起始标签就表示设置值为`true`。
<option selected="selected">...</option>
In HTML, boolean attributes may appear in minimized form -- the attribute's value appears alone in the element's start tag.
Thus, selected may be set by writing:
<OPTION selected>
instead of:
<OPTION selected="selected">
Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.
在HTML中,boolean attribute
可以最小化表示:attribute
单独出现在元素的开始标签中。
因此,selected
可以用以下写法设置:
<option selected>
相当于:
<option selected="selected">
注意:许多用户代理只识别boolean attribute
的最小化形式,而不是完全形式。