boolean attribute及其规范取值

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的最小化形式,而不是完全形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值