Option index 屬性
實例
例子 1
顯示在下拉列表中所選選項的索引和文本:
var x = document.getElementById("mySelect").selectedIndex; var y = document.getElementById("mySelect").options; alert("Index: " + y[x].index + " is " + y[x].text);
例子 2
顯示下拉列表中所有選項的文本和索引:
var x = document.getElementById("mySelect"); var txt = "All options: "; var i; for (i = 0; i < x.length; i++) { txt = txt + "\n" + x.options[i].text + " has index: " + x.options[i].index; }
語法
返回 index 屬性:
optionObject.index
設置 index 屬性:
optionObject.index = integer
屬性值
值 | 描述 |
---|---|
integer | 規定下拉列表中選項的索引位置。 |
技術細節
返回值: | 數值,表示下拉列表中選項的索引位置。索引從 0 開始。 |
---|
瀏覽器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |