是 autofocus=“autofocus”吗?或者自动对焦?
我似乎记得以前版本的 HTML(HTML5 之前)中的大多数(也许是全部)属性都要求属性具有值,例如 readonly="readonly"
。
对于 HTML5 和 autofocus
属性来说是这样吗?
I seem to recall most (maybe all) attributes in previously versions of HTML (before HTML5) required attributes to have values, like readonly="readonly"
.
Is this true for HTML5 and the autofocus
attribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 HTML 中,您可以根据需要使用带或不带值的布尔属性。
对于 W3C 来说,布尔值(例如 autofocus)可以这样编写:
autofocus
或autofocus="autofocus"
或autofocus=""
。如果你不想自动对焦就不要写它。
我认为您很困惑,因为 XHTML 需要所有属性的值:
attributes="values"
。以下是有关 HTML 中布尔属性使用的一些信息:
http://www.whatwg .org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute
In HTML, you use boolean attributes with or without values as you like.
A boolean, for W3C, like autofocus can be written like that
autofocus
orautofocus="autofocus"
or alsoautofocus=""
.If you don't want autofocus just don't write it.
I think you are confused because XHTML requires values for all attributes:
attributes="values"
.Here is some information about boolean attribute use in HTML:
http://www.whatwg.org/specs/web-apps/current-work/multipage/common-microsyntaxes.html#boolean-attribute
引用HTML5规范并在Pekka上进行一些扩展:
http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :
http://www.w3.org/TR/html5/infrastruct.html #boolean-attributes:
结论:
以下是有效、等价和正确:
以下是无效:
缺少该属性是唯一有效的语法>false:
建议
如果您关心编写有效的 XHTML,请使用
autofocus="autofocus"
,因为是无效的,其他替代方案的可读性较差。否则,只需使用
因为它更短。
Quoting the HTML5 spec and expanding a bit on Pekka:
http://www.w3.org/TR/html5/forms.html#autofocusing-a-form-control:-the-autofocus-attribute :
http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :
Conclusion:
The following are valid, equivalent and true:
The following are invalid:
The absence of the attribute is the only valid syntax for false:
Recommendation
If you care about writing valid XHTML, use
autofocus="autofocus"
, since<input autofocus>
is invalid and other alternatives are less readable. Else, just use<input autofocus>
as it is shorter.否,指定属性本身。 HTML 4 也是如此。
例子:
No, it's enough to specify the attribute itself. It was that way also in HTML 4.
Example: