Html “option”标签的“selected”属性中可以出现哪些值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1033944/
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
What values can appear in the "selected" attribute of the "option" tag?
提问by Travis Beale
I have some markup similar to the following:
我有一些类似于以下的标记:
<select>
<option selected="selected">Apple</option>
<option selected="">Orange</option>
</select>
In this case, "Orange" shows as the selected item. I would have expected making the selected
attribute blank would undo its effects. Is there a way to write this without simply leaving the attribute out?
在这种情况下,“橙色”显示为所选项目。我原以为将selected
属性设为空白会撤消其效果。有没有办法在不简单地忽略属性的情况下编写它?
采纳答案by LBushkin
Different browser may treat this attribute differently. According to the MSDN documentation (for Internet Explorer):
不同的浏览器可能对这个属性有不同的处理。根据 MSDN 文档(适用于 Internet Explorer):
To select an item in HTML, it is not necessary to set the value of the SELECTED attribute to true. The mere presence of the SELECTED attribute set its value to true.
要选择 HTML 中的项目,不必将 SELECTED 属性的值设置为 true。仅存在 SELECTED 属性就将其值设置为 true。
In firefox and Safari this does work:
在 Firefox 和 Safari 中,这确实有效:
<option selected='false' />
From what I can tell by looking at the official WC3 standard for HTML4, the supported case is only:
从我通过查看 HTML4 的官方 WC3 标准可以看出,支持的情况只有:
<option selected='selected' />
You will either need to selectively emit the attribute, or use javascript to control which item is initially selected.
您要么需要有选择地发出属性,要么使用 javascript 来控制最初选择哪个项目。
回答by Rony
the only allowed value for selected attribute in XHTML is "selected" so if you want your markup to be XHTML compliant and work across all browsers leaving it out is the only choice to make it unselected
XHTML 中 selected 属性的唯一允许值是“ selected”,因此如果您希望您的标记符合 XHTML 并在所有浏览器中工作,则将其排除在外是唯一的选择以使其取消选择
回答by jimyi
Nope, the existence of the selected attribute tells the browser that it is the selected item. Anything inside the quotes is ignored.
不, selected 属性的存在告诉浏览器它是所选项目。引号内的任何内容都将被忽略。
Edit: What you could do (with Javascript) is look for option tags with selected="", and remove the selected attribute from them.
编辑:您可以(使用 Javascript)做的是查找带有 selected="" 的选项标签,并从中删除 selected 属性。
回答by TRiG
In HTML (as opposed to XHTML) a simple selected
attribute, with no value at all, works fine:
在 HTML(与 XHTML 相对)中,一个selected
没有任何值的简单属性可以正常工作:
<option selected>Apple</option>
<option>Orange</option>
In XHTML (including XHTML5) you need a value, which should also be selected
:
在 XHTML(包括 XHTML5)中,您需要一个值,它也应该是selected
:
<option selected="selected">Apple</option>
<option>Orange</option>
This also works fine in HTML.
这在 HTML 中也能正常工作。
This is generally the case for boolean values in (X)HTML. The way to set them to false is to omit them altogether. Setting values of true
and false
may work, but is nonstandard.
这通常是 (X)HTML 中的布尔值的情况。将它们设置为 false 的方法是完全省略它们。设置的值true
,并false
可以工作,但是是非标准的。
Note that for a list of options, the first is selected by default, so this isn't necessary at all in this case.
请注意,对于选项列表,默认情况下会选择第一个,因此在这种情况下根本不需要。
回答by Chet
There aren't any other valid values other than "selected" for that attribute. (http://www.w3schools.com/TAGS/att_option_selected.asp)
除了该属性的“selected”之外,没有任何其他有效值。( http://www.w3schools.com/TAGS/att_option_selected.asp)
回答by amischiefr
回答by Jeff Meatball Yang
You are better off setting the selectElement.selectedIndex property from Javascript, or removing the attribute altogether.
您最好从 Javascript 设置 selectElement.selectedIndex 属性,或者完全删除该属性。