[英]Why is it that when I resize my select the first element becomes selected?
If the size
of a <select>
tag is dynamically changed from JavaScript, the first element becomes automatically selected if the initial size
attribute was either 0
or 1
(MSEdge, Win10).如果
<select>
标记的size
从 JavaScript 动态更改,则如果初始size
属性为0
或1
(MSEdge,Win10),则自动选择第一个元素。
<select id="s" size="1"> <option>a <option>b <option>c </select> <select size="3"> <option>a <option>b <option>c </select> <script> s.size=3; </script>
I imagine a size
of 0 or 1 forces a selection, but I can't see why this is, as I didn't ask it to.我想 0 或 1 的
size
会强制进行选择,但我不明白为什么会这样,因为我没有要求它这样做。
Does anyone have an explanation?有人有解释吗?
This is actually covered in the specification :这实际上包含在规范中:
If nodes are inserted or nodes are removed causing the list of options to gain or lose one or more option elements, or if an option element in the list of options asks for a reset, then, if the select element's multiple attribute is absent, the user agent must run the first applicable set of steps from the following list:
如果插入节点或删除节点导致选项列表获得或丢失一个或多个选项元素,或者如果选项列表中的选项元素要求重置,则如果 select 元素的多属性不存在,则用户代理必须运行以下列表中的第一组适用步骤:
- If the select element's display size is 1, and no option elements in the select element's list of options have their selectedness set to true
如果 select 元素的显示大小为 1,并且 select 元素的选项列表中没有选项元素的选择度设置为 true
- Set the selectedness of the first option element in the list of options in tree order that is not disabled, if any, to true.
将选项列表中未禁用(如果有)的第一个选项元素的选中状态设置为 true。
- If two or more option elements in the select element's list of options have their selectedness set to true
如果 select 元素的选项列表中的两个或多个选项元素的选择性设置为 true
- Set the selectedness of all but the last option element with its selectedness set to true in the list of options in tree order to false.
在按树顺序排列的选项列表中,将除最后一个选项元素之外的所有选项元素的选中性设置为 true,并将其选中性设置为 true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.