html label 標簽的 for 屬性


如果您在 label 元素內點擊文本,就會觸發此控件。就是說,當用戶選擇該標簽時,瀏覽器就會自動將焦點轉到和標簽相關的表單控件上。

有兩種使用方法:

方法1  使用for屬性

<label for="userName">用戶名:</label><input type="text" id="userName"/>  

方法2

<label>用戶名:<input type="text"/></label> 

在使用單選按鈕radio或者多選按鈕checkbox的時候,有時需要在選中文字的時候就要選中單選或者多選按鈕,這同樣可以使用類似上面的方法。

方法1

<label><input type="radio" />用戶名</label> 
<label><input type="checkbox">學校</label>

方法2  使用for屬性

<label for="userName">用戶名</label> <input type="radio" id="userName" />

<label for="userName">用戶名</label> <input type="checkbox" id="userName" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM