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

瀏覽器相容性

另見