html label 標籤的 for 屬性

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

有兩種使用方法:spa

方法1  使用for屬性code

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

方法2blog

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

在使用單選按鈕radio或者多選按鈕checkbox的時候,有時須要在選中文字的時候就要選中單選或者多選按鈕,這一樣可使用相似上面的方法。input

方法1 io

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

方法2  使用for屬性class

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

<label for="userName">用戶名</label> <input type="checkbox" id="userName" />
相關文章
相關標籤/搜索