JavaScript selectedIndex
selectedIndex是可讀寫屬性,具有如下兩個功能:
(1).返回被選中option項的索引值。
(2).將指定索引位置的option項設定為被選中狀態。
特別說明:索引值是從0開始的,也就是0表示第一個option項,以此類推。
語法結構:
[JavaScript] 純文字檢視 複製程式碼select.selectedIndex=number
引數解析:
(1).select:select元素物件。
(2).number:一個數字,設定指定索引位置option項被選中。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload=function(){ let oSelect=document.getElementById("st"); let odiv=document.getElementById("ant"); odiv.innerHTML=oSelect.selectedIndex; } </script> </head> <body> <select id="st" name="myselect"> <option value="1">螞蟻部落一</option> <option selected value="2">螞蟻部落二</option> <option value="3">螞蟻部落三</option> <option value="4">螞蟻部落四</option> </select> <div id="ant"></div> </body> </html>
上述程式碼可以將被選中的option項的索引位置寫入div中。
[HTML] 純文字檢視 複製程式碼執行程式碼<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <script> window.onload=function(){ let oSelect=document.getElementById("st"); oSelect.selectedIndex=2; } </script> </head> <body> <select id="st" name="myselect"> <option value="1">螞蟻部落一</option> <option selected value="2">螞蟻部落二</option> <option value="3">螞蟻部落三</option> <option value="4">螞蟻部落四</option> </select> </body> </html>
上述程式碼可以將索引位置為2的option選項設定為當前選中狀態。
相關文章
- JavaScript高階:JavaScript物件導向,JavaScript內建物件,JavaScript BOM,JavaScript封裝JavaScript物件封裝
- javaScript系列[06]-javaScript和thisJavaScript
- [Javascript] How javascript read the property?JavaScript
- javaScript系列[05]-javaScript和JSONJavaScriptJSON
- “This” is For JavaScriptJavaScript
- This in JavaScriptJavaScript
- JavaScript -"this"JavaScript
- JavaScriptJavaScript
- javascript ??JavaScript
- 44 道 JavaScript 難題(JavaScript Puzzlers!)JavaScript
- [Javascript] Understanding JavaScript Proxies with Symbol.toPrimitiveJavaScriptSymbolMIT
- JavaScript 教程之JavaScript常用框架簡介JavaScript框架
- [Javascript] Perform Set Operations using JavaScript Set MethodsJavaScriptORM
- javascript — == vs ===JavaScript
- JavaScript deleteCell()JavaScriptdelete
- JavaScript lastElementChildJavaScriptAST
- JavaScript hasAttribute()JavaScript
- JavaScript getAttributeNode()JavaScript
- JavaScript replaceChild()JavaScript
- JavaScript remove()JavaScriptREM
- JavaScript appendChild()JavaScriptAPP
- JavaScript deleteRow()JavaScriptdelete
- JavaScript clientYJavaScriptclient
- JavaScript之thisJavaScript
- JavaScript isFinite()JavaScript
- JavaScript toggle()JavaScript
- JavaScript Boolean()JavaScriptBoolean
- JavaScript escape()JavaScript
- JavaScript parseFloat()JavaScript
- JavaScript unescape()JavaScript
- JavaScript call()JavaScript
- JavaScript bind()JavaScript
- JavaScript previousElementSiblingJavaScript
- JavaScript preventDefault()JavaScript
- JavaScript insertRow()JavaScript
- JavaScript isPrototypeOf()JavaScript
- JavaScript removeChild()JavaScriptREM
- JavaScript nextElementSiblingJavaScript