gpt4 book ai didi

html - "selected"标签的 "option"属性中可以出现什么值?

转载 作者:技术小花猫 更新时间:2023-10-29 11:43:35 25 4
gpt4 key购买 nike

我有一些类似于以下的标记:

<select>
<option selected="selected">Apple</option>
<option selected="">Orange</option>
</select>

在这种情况下,“橙色”显示为所选项目。我原以为将 selected 属性设为空白会取消其效果。有没有一种方法可以在不简单地保留属性的情况下编写它?

最佳答案

HTML5 规范

https://www.w3.org/TR/html51/sec-forms.html#the-option-element

The selected content 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>无效,其他替代方案的可读性较差。否则,只需使用 <option selected>因为它更短。

关于html - "selected"标签的 "option"属性中可以出现什么值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1033944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com