HTML DOM Select selectedIndex 屬性
HTML DOM select selectedIndex 屬性會返回和修改 HTML 文件中下拉列表的選定選項的索引。
語法
以下是語法 −
返回 selectedIndex
object.selectedIndex
修改 selectedIndex
object.selectedIndex = “number”
示例
讓我們看一個 HTML DOM select selectedIndex 屬性的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: radial-gradient( circle farthest-corner at 23.1% 64.6%, rgba(129,125,254,1) 0%, rgba(111,167,254,1) 90% ) no-repeat;
height:100%;
}
p{
font-weight:700;
font-size:1.1rem;
}
.drop-down{
display:block;
width:35%;
border:2px solid #fff;
font-weight:bold;
padding:2px;
margin:1rem auto;
outline:none;
}
.btn{
background:orange;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
font-weight:bold;
outline:none;
cursor:pointer;
}
.show{
font-size:1.5rem;
font-weight:bold;
}
</style>
</head>
<body>
<h1>DOM Select selectedIndex property Demo</h1>
<p>Hi, Select your favourite subject:</p>
<select class='drop-down' name="Drop Down List">
<option>Physics</option>
<option>Maths</option>
<option>Chemistry</option>
<option>English</option>
<option>Economics</option>
<option>Hindi</option>
<option>Biology</option>
</select>
<button type="button" onclick="set()" class="btn">Set selectedIndex</button>
<div class="show"></div>
<script>
function set() {
var dropDown = document.querySelector(".drop-down");
document.querySelector(".show").innerHTML="put selectedIndex = '3'";
dropDown.selectedIndex="3";
}
</script>
</body>
</html>輸出
這會產生以下輸出 −

點選“設定 selectedIndex”按鈕,對下拉列表設定 selectedIndex=’3’。

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP