Selected 是 <option> 元素的一個屬性,用於定義 dropdown-list 中的默認選擇。
當任何選項使用此屬性時,應在網頁加載時預先選擇並顯示該選項。
用法
<option selected>
例:以下示例使用帶有 Jaguar 值的 <option> 標記中的 Selected 屬性。
<!DOCTYPE html>
<html>
<head>
<title>
Example of selected attribute
</title>
</head>
<body>
<h4> Select Your favourite Car from the following given options:</h4>
<br>
<select name="cars">
<option value="Mercedes"> Mercedes </option>
<option value="Lamborghini"> Lamborghini </option>
<!-- In the following option tag we specify the selected attribute for the Jaguar value, which is display as selected on the web page by default. -->
<option value="Jaguar" selected> Jaguar </option>
<option value="BMW" > BMW </option>
<option value="Audi"> Audi </option>
</select>
</body>
</html>
輸出:

範例2:以下示例將 <option> 標記中的 Selected 屬性與 Pomegranate 值一起使用。
<!DOCTYPE html>
<html>
<head>
<title>
Second Example of selected attribute
</title>
</head>
<body>
<h4> Select Your favourite fruit from the following list of fruits:</h4>
<br>
<select name="fruits">
<option value="Apple"> Apple </option>
<option value="Banana"> Banana </option>
<option value="Orange"> Orange </option>
<!-- In the following option tag we specify the selected attribute for the Pomegranate value, which is display as selected on the web page by default.-->
<option value="Pomegranate" selected> Pomegranate </option>
<option value="Grapes"> Grapes </option>
<option value="Mango"> Mango </option>
</select>
</body>
</html>
輸出:

瀏覽器支持
Element | ![]() | ![]() | ![]() | ![]() | ![]() |
<selected> | Yes | Yes | Yes | Yes | Yes |
相關用法
- HTML Select form用法及代碼示例
- HTML Select selectedIndex用法及代碼示例
- HTML Select length用法及代碼示例
- HTML Select multiple用法及代碼示例
- HTML Select type用法及代碼示例
- HTML Select size用法及代碼示例
- HTML Select disabled用法及代碼示例
- HTML Select value用法及代碼示例
- HTML Select autofocus用法及代碼示例
- HTML Select name用法及代碼示例
- HTML DOM Select remove()用法及代碼示例
- HTML Style marginRight用法及代碼示例
- HTML Style transformOrigin用法及代碼示例
- HTML Style borderImageWidth用法及代碼示例
- HTML Style flex用法及代碼示例
- HTML Screen width用法及代碼示例
- HTML Script text用法及代碼示例
- HTML Style textTransform用法及代碼示例
- HTML Screen pixelDepth用法及代碼示例
- HTML Style borderBottomWidth用法及代碼示例
注:本文由純淨天空篩選整理自 HTML Selected Attribute。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。