html - 如何为选项 HTML 标记标记选定的选项?

标签 html css drop-down-menu

我在我的网站上使用普通的选择框和多选框。 我应该使用 <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/

相关文章:

asp.net - 数据绑定(bind)下拉列表 - 初始值

javascript - 如何在不创建数组的情况下使用 NgFor 来生成矩阵 UI 模式

javascript - 使用 Javascript 将日期/日期时间从一个 Html 输入复制到另一个 Html 输入

javascript - 如何使用点击事件检测 Canvas 上的对象

html - 页面内容 div 和最小高度

css - 如何将粘性菜单放在 slider 顶部

javascript - 多级下拉菜单,下拉问题

PHP - 下拉菜单(选择)无法正常工作到 while 循环

javascript - 跨浏览器选择框多个选项,无需按住控件

html - ion-content : padding or no padding?