當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


HTML Selected屬性用法及代碼示例

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>

輸出:

HTML Selected Attribute

範例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>

輸出:

HTML Selected Attribute

瀏覽器支持

Elementchrome browser Chromeie browser IEfirefox browser Firefoxopera browser Operasafari browser Safari
<selected>YesYesYesYesYes




相關用法


注:本文由純淨天空篩選整理自 HTML Selected Attribute。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。