简体   繁体   English

为什么当我调整 select 的大小时,第一个元素被选中?

[英]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属性为01 (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.

相关问题 Material React Select Multiple:单击选定元素时如何从数组中删除? - Material React Select Multiple: How i can remove from my array when i click in a selected element? 当我有一个隐藏的字段(如果选择了一个选项时会显示)时,我无法获得select元素的值 - I can't get the value of my select element when i have a hidden field that is revealed if an option is selected 为什么我在浏览器上调整大小时页面会滚动? - Why my page scroll when I resize on my browser? 当事件变为“ true”时,为什么没有在克隆的元素上触发我的JQuery函数? - Why is my JQuery function not triggered on a cloned element when the event becomes “true”? 选择多个/所有选项时,选择多个选择变得很大 - Chosen multiple select becomes huge when many/all options are selected 为什么选择在绑定到模型时首先显示它的元素 - Why select is showing first of it's element when it's binded to model 当我将光标移出画布时,为什么我的图形不可见? - Why does my drawing becomes invisible when I take my cursor out of the canvas? 我为什么不 <div> 我告诉它用javascript调整大小? - why won't my <div> resize with javascript when I tell it to? 当我使用冒泡排序时,为什么我将数组中的最高值作为我的第一个元素? - why do i get the highest valued number in my array as my first element when i use bubble sort? 选择一个元素后,无法选择其他元素 - Disable to select other element when an element is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM