Html 在 html5 中将复选框设置为选中状态,我应该简单地使用选中的(作为属性)还是选中的 =“选中”(作为属性)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12331290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In html5 to set a checkbox as checked, should i simply use checked ( as a property) or checked="checked" ( as an attribute)?
提问by Nicola Peluchetti
Currently in our plugin we were setting the checkboxes as checked by setting
目前在我们的插件中,我们将复选框设置为通过设置检查
<input type="checkbox" checked="checked" />
This was to preserve xhtml compatibility. I'm more used to setting checked as a property
这是为了保持 xhtml 兼容性。我更习惯于将检查设置为属性
<input type="checkbox" checked />
What is the correct way to proceed in html5?Should we still care about xhtml compatibility?
在html5中进行的正确方法是什么?我们还应该关心xhtml兼容性吗?
采纳答案by Jukka K. Korpela
It is an attribute in either case. And it sets a value (the same value, true
) on a DOM property of the element node in either case.
在任何一种情况下,它都是一个属性。true
在任何一种情况下,它都会在元素节点的 DOM 属性上设置一个值(相同的值,)。
For most purposes, it does not matter which syntax you use. However, there are some points to note:
对于大多数目的,使用哪种语法并不重要。但是,有几点需要注意:
- If you use HTML5 in XML serialization (“XHTML5”), you must use
checked="checked"
. - In styling, the syntaxes are not quite equivalent when using attribute selectors (the shorter form does not match
[checked=checked]
), but this does not matter in practice:[checked]
matches checked checkboxes in either case. - The clumsy syntax
checked="checked"
is a holdover from SGML and included for compatibility only, so it may make your code look old-fashioned (which rarely matters).
- 如果在 XML 序列化(“XHTML5”)中使用 HTML5,则必须使用
checked="checked"
. - 在样式方面,使用属性选择器时的语法并不完全相同(较短的形式不匹配
[checked=checked]
),但这在实践中并不重要:[checked]
在任何一种情况下都匹配选中的复选框。 - 笨拙的语法
checked="checked"
是 SGML 的保留,仅用于兼容性,因此它可能会使您的代码看起来过时(这很少重要)。
回答by Jukka K. Korpela
<!-- Default to unchecked -->
<input type="checkbox">
<!-- Default to checked, XHTML -->
<input type="checkbox" checked="checked" />
<!-- Default to checked, HTML5 -->
<input type="checkbox" checked>
回答by jncraton
Checked
is a boolean attribute in HTML 5. A true value is indicated by the attribute being present, and a false value is indicated by its absence. If it is present, its value should either be empty or set to the property name checked="checked"
. Either of these forms are correct:
Checked
是 HTML 5 中的布尔属性。真值由属性存在表示,假值由属性不存在表示。如果存在,则其值应为空或设置为属性 name checked="checked"
。以下任何一种形式都是正确的:
<input type="checkbox" checked="checked" />
<input type="checkbox" checked>
https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes
https://www.w3.org/TR/html5/infrastructure.html#sec-boolean-attributes
回答by Alohci
You care about XHTML-compatibility in HTML5, if you are creating documents that use the XHTML serialization of HTML5, either exclusively by serving the document with an application/xhtml+xml
mime type, or creating a polyglot document that can be served either as application/xhtml+xml
or as text/html
(the 'normal' html mime-type).
您关心 HTML5 中的 XHTML 兼容性,如果您正在创建使用 HTML5 的 XHTML 序列化的文档,或者专门通过使用application/xhtml+xml
mime 类型提供文档,或者创建一个可以作为application/xhtml+xml
或作为text/html
('普通' html mime 类型)。
If you are only using text/html
, then you do not need to care about XHTML syntax. However, you mayuse XML-style self-closing syntax when embedding SVG or MathML in your page. (SVG is widely supported in the latest browsers, MathML less so.) You may also use />
to end voidHTML elements such as meta, link, input, img etc, but this has no effect different from using >
to end those elements.
如果您只使用text/html
,那么您无需关心 XHTML 语法。但是,在页面中嵌入 SVG 或 MathML 时,您可以使用 XML 样式的自关闭语法。(SVG 在最新的浏览器中得到广泛支持,MathML 较少支持。)您也可以使用/>
结束无效的HTML 元素,例如 meta、link、input、img 等,但这与使用>
结束这些元素没有什么不同。
A minor comment on terminology. In markup, in common parlance either checked
or checked="checked"
is an "attribute". A "property" is something else.
对术语的小评论。在标记中,按照一般的说法任一checked
或checked="checked"
是一个“属性”。“财产”是另一回事。
回答by Dirk McQuickly
According to http://www.w3.org/TR/html-markup/input.checkbox.htmlit's an attribute
根据http://www.w3.org/TR/html-markup/input.checkbox.html它是一个属性