我在我的网站上使用普通的选择框和多选框。
我应该使用 <option selected="selected">
吗?或者简单地 <option selected>
对于选定的元素?
最佳答案
HTML5 规范:
https://www.w3.org/TR/html5/forms.html#attr-option-selected
The selected 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.
结论:
以下是有效、等价和真实:
<option selected />
<option selected="" />
<option selected="selected" />
<option selected="SeLeCtEd" />
以下是无效:
<option selected="0" />
<option selected="1" />
<option selected="false" />
<option selected="true" />
缺少属性是 false 的唯一有效语法:
<option />
推荐
如果您关心编写有效的 XHTML,请使用 selected="selected"
, 自 <option selected>
是无效的 XHTML(但有效的 HTML)并且其他替代方案的可读性较差。否则,只需使用 <option selected>
因为它更短。
关于html - 如何为选项 HTML 标记标记选定的选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10881448/