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


HTML <label>用法及代碼示例

HTML中的<label>標記用於為鼠標用戶提供可用性改進,即,如果用戶單擊<label>元素內的文本,它將切換控件。 <label>標簽為<button>,<input>,<meter>,<output>,<progress>,<select>或<textarea>元素定義標簽。

<label>標記可以兩種方式使用:

  • 首先,通過提供<input>和id屬性來使用<label>標簽。 <label>標記需要一個for屬性,該屬性的值與輸入id相同。
  • 另外,<input>標記可直接在<label>標記內使用。在這種情況下,因為關聯是隱式的,所以不需要for和id屬性。

用法:

<label> form content... </label>

屬性值:

範例1:在這裏,我們將在level標簽之外使用input標簽。



html

<!DOCTYPE html> 
<html> 
  
<body> 
  
    <h1>GeeksforGeeks</h1> 
    <strong>HTML label Tag</strong> 
  
    <form> 
  
        <!-- Starts label tags from here -->
        <label for="student"> 
                Student 
        </label> 
        <input type="radio" name="Occupation"
               id="student" value="student"><br> 
  
        <label for="business"> 
                Business 
        </label> 
        <input type="radio" name="Occupation"
               id="business" value="business"><br> 
  
        <label for="other"> 
                Other 
        </label> 
        <!-- Ends label tags here -->
  
        <input type="radio" name="Occupation" 
               id="other" value="other"> 
    </form> 
</body> 
  
</html>

輸出:

範例2:在這裏,我們將在level標簽內使用input標簽。

html

<!DOCTYPE html> 
<html> 
  <body> 
    <h1>GeeksforGeeks</h1> 
  
    <strong>HTML label Tag </strong> 
  
    <form> 
      <!-- label tag starts from here -->
      <label> 
        Male 
        <input type="radio" name="gender" 
               id="male" value="male" /> 
      </label><br/> 
  
      <label> 
        Female 
        <input type="radio" name="gender" 
               id="female" value="female" />  
      </label><br/> 
  
      <label> 
        Other 
        <input type="radio" name="gender" 
               id="other" value="other" /> 
      </label> 
      <!-- label tag ends from here -->
    </form> 
  </body> 
</html>

輸出:

支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari




相關用法


注:本文由純淨天空篩選整理自Vishal Chaudhary 2大神的英文原創作品 HTML <label> Tag。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。