如果您在 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" />