国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

html5中<label>標簽的for屬性使用方式方法詳細分

2024-08-26 00:15:28
字體:
來源:轉載
供稿:網友
介紹的內容是html5中<label>標簽的for屬性使用方式方法詳細分析。
本站收錄這篇文章html5中<label>標簽的for屬性使用方式方法詳細分析,詳細解說文章中相關html5 label 標簽 for屬性 技術與知識,歡迎能給大家一些在這方面的支持和幫助!下面是詳細內容:

介紹的內容是html5中<label>標簽的for屬性使用方式方法詳細分析。

在freecodecamp上HTML教程的Create a Set of Radio Buttons這一節中,看到這樣一段話,

It is considered best practice to set a for attribute on the label element, with a value that matches the value of the id attribute of the input element. This allows assistive technologies to create a linked relationship between the label and the child input element.

大概的意思是:最好的做法,是給label標簽,添加for屬性,其值與input標簽的id屬性的值相同,以在label和input之間創建關聯。

同時,給出一段示例代碼,如下:

<!-- Code 1 --><label for="indoor">   <input id="indoor" type="radio" name="indoor-outdoor">Indoor </label>

代碼中,label的for屬性值與input的id屬性值相同。從這段代碼中,并不能看出關聯在何處。

關于label的for屬性的定義如下:

The for attribute specifies which form element a label is bound to.
譯文:for屬性指定label與表單中的哪個元素進行綁定。

示例代碼:

<!-- Code 2 --><form action="/action_page.php">  <input type="radio" name="gender" id="male" value="male">  <label for="male">Male</label>  <br>  <input type="radio" name="gender" id="female" value="female">  <label for="female">Female</label>  <br>  <input type="radio" name="gender" id="other" value="other">  <label for="other">Other</label>  <br>  <input type="submit" value="Submit"></form>

 

對比兩段代碼,不難發現,

  1. label與input標簽的包含關系不同。Code 1 的label和input,屬于包含關系,Code 2 的label和input相對獨立。

  2. label與input在頁面上的排列方式不同。通過Chrome的開發者工具不難發現,Code 1 的運行結果,label標簽將input標簽遮蓋,Code 2 的運行結果,label標簽與input標簽并列。

  3. label與input一一對應。點擊label的內容,對應的單端按鈕都會被選中。

如果,我們將兩段代碼中label的for屬性刪除,則之前的第1點和第2點不變,變化的是第3點。Code 1 的運營結果,點擊label的內容,照舊能夠選中單選按鈕。而 Code 2 的則不同,點擊label的內容,無法選中單選按鈕。

經過簡單的代碼運行結果對比,我們能夠驗證文章開頭引用的那段話是正確的。為label添加for屬性的這個做法,能夠提高代碼質量。


關于html5中<label>標簽的for屬性使用方式方法詳細分析的內容寫到這里就結束啦,您可以收藏本頁網址http://www.survivalescaperooms.com/web/a/2018090496006.shtml方便下次再訪問哦。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安化县| 满城县| 永昌县| 白河县| 宝应县| 若尔盖县| 鄂伦春自治旗| 大埔区| 扶余县| 桐柏县| 隆子县| 西平县| 连南| 肃宁县| 鱼台县| 正安县| 濮阳县| 池州市| 翁牛特旗| 军事| 什邡市| 额尔古纳市| 巴彦淖尔市| 哈尔滨市| 广元市| 武强县| 奉新县| 宜春市| 旬阳县| 门源| 乐东| 武定县| 翁牛特旗| 磐石市| 深泽县| 台江县| 乡宁县| 涟源市| 邓州市| 金川县| 儋州市|