HTMLOptionsCollection: selectedIndex 属性

HTMLOptionsCollection 接口的 selectedIndex 属性是第一个选中的 <option> 元素的数字索引;如果没有选中的元素,则为 -1。设置此属性会选中该索引处的选项,并取消选中此集合中的所有其他选项;将其设置为 -1 则会取消选中当前所有选中的元素。此属性与拥有此集合的 HTMLSelectElementselectedIndex 属性完全等效。

一个数字。

示例

js
const optionColl = document.getElementById("select").options;
console.log(`selected option: ${optionColl.selectedIndex}`); // the index of the first selected option, or -1 if no option is selected
optionColl.selectedIndex = 0; // selects the first item
optionColl.selectedIndex = -1; // deselects any selected option

规范

规范
HTML
# the-htmloptionscollection-interface:dom-htmloptionscollection-selectedindex-2

浏览器兼容性

另见