是 autofocus=“autofocus”吗?或者自动对焦?

发布于 2024-10-07 19:31:00 字数 143 浏览 7 评论 0原文

我似乎记得以前版本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羅雙樹 2024-10-14 19:31:00

在 HTML 中,您可以根据需要使用带或不带值的布尔属性。
对于 W3C 来说,布尔值(例如 autofocus)可以这样编写:autofocusautofocus="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 or autofocus="autofocus" or also autofocus="".

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

半枫 2024-10-14 19:31:00

引用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

元素上存在布尔属性表示真值,不存在该属性表示假值。

如果该属性存在,则其值必须是空字符串或与该属性的规范名称匹配的 ASCII 不区分大小写的值,并且没有前导或尾随空格。

结论

以下是有效、等价和正确

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

以下是无效

<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />

缺少该属性是唯一有效的语法>false

<input type="text"/>

建议

如果您关心编写有效的 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 :

The autofocus attribute is a boolean attribute.

http://www.w3.org/TR/html5/infrastructure.html#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.

Conclusion:

The following are valid, equivalent and true:

<input type="text" autofocus />
<input type="text" autofocus="" />
<input type="text" autofocus="autofocus" />
<input type="text" autofocus="AuToFoCuS" />

The following are invalid:

<input type="text" autofocus="0" />
<input type="text" autofocus="1" />
<input type="text" autofocus="false" />
<input type="text" autofocus="true" />

The absence of the attribute is the only valid syntax for false:

<input type="text"/>

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.

成熟稳重的好男人 2024-10-14 19:31:00

,指定属性本身。 HTML 4 也是如此。

许多属性都是布尔属性。元素上存在布尔属性表示真值,不存在该属性表示假值。

如果该属性存在,则其值必须是空字符串或与该属性的规范名称匹配的 ASCII 不区分大小写的值,并且没有前导或尾随空格。

例子:

<label><input type=checkbox checked name=cheese disabled> Cheese</label>

No, it's enough to specify the attribute itself. It was that way also in HTML 4.

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.

Example:

<label><input type=checkbox checked name=cheese disabled> Cheese</label>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文