HTML Boolean Attributes_html/css_WEB-ITnose

php中文网
发布: 2016-06-21 08:48:15
原创
1588人浏览过

有时在阅读别人的 html 代码时,会发现在写 / <input type="checkbox">/

前几天,我突然发现我没有搞清楚 HTML 布尔属性假如有值的话,哪些是合法值,我竟然认为 空字符串和 "false"表示 假值,然后看规范才明白不是这样。

HTML 布尔属性(Boolean attributes)

HTML5 规范说:

… A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute’s canonical name, with no leading or trailing whitespace.

立即学习前端免费学习笔记(深入)”;

以及:

The values “true” and “false” are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

所以,如果要表示 <input type="checkbox">选中的写法有:

<input type="checkbox" checked><input type="checkbox" checked="checked"><input type="checkbox" checked="">
登录后复制

如果表示不选中,则不能出现 checked属性:

<input type="checkbox">
登录后复制

个人对于空字符串可作为合法值不太赞同,感觉会给人一种不选中的误导。

checked与 checked="checked"区别

在 W3C 的 HTML 4.01 规范中已经有 Boolean attributes 的说明,里面提到了:

html表单框架代码
html表单框架代码

html表单框架代码,输入框文字提示上浮css3动画,在您需要输入文字时,输入框内的文字提示会上浮显示,不仅可以提示又不影响输入,设计细节非常人性化。

html表单框架代码 40
查看详情 html表单框架代码

Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.

当然,现在主流的浏览器都支持 checked="checked"这种写法的,所以实际使用效果没有区别。

其区别主要在于: checked字符少,简洁直观;而使用 checked="checked"的理由主要是在 XHTML 中,只写 checked不合法,但是我们通常是在写 HTML5,所以没什么理由写 checked="checked"。

布尔属性(Boolean attributes)列表

截止该文章书写时有 39 个:

allowFullscreenasyncautofocusautoplaycheckedcompactcontrolsdeclaredefaultdefaultCheckeddefaultMuteddefaultSelecteddeferdisableddraggableenabledformNoValidatehiddenimageSmoothingEnabledindeterminateisMaploopmultiplemutednoHrefnoResizenoShadenoValidatenoWrapopenpauseOnExitreadOnlyrequiredreversedselectedspellchecktranslatetrueSpeedtypeMustMatch
登录后复制

上面列表获取方法:WHATWG HTML 规范在 Github 上面的仓库( https://github.com/whatwg/html)

git clone https://github.com/whatwg/htmlcd html# cat source | grep "attribute boolean" 之后手工处理,或执行下面代码cat source | grep "attribute boolean" | grep -v "readonly attribute boolean" | grep -oE "\">[A-Za-z]{1,}</span>" | awk -F'>|<' '{print $2}' | sort | uniq
登录后复制

Boolean attributes in React

React的 JSX 语法与 HTML 有所不同,在 JSX 中,可以传入 true表示设置该布尔属性,传入 false表示不设置该属性,如:

表示选中复选框:

<input type="checkbox" checked /><input type="checkbox" checked={true} />
登录后复制

表示不选中复选框:

<input type="checkbox" /><input type="checkbox" checked={false} />
登录后复制

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号