2013-09-22 41 views
13

我不知道在檢查我的網頁在http://validator.w3.org/check錯誤是爲什麼我不斷收到此錯誤:驗證HTML:該爲標籤元素必須引用表單控件的屬性

Line 235, Column 84: The for attribute of the label element must refer to a form control. 
… <label for="name" style="line-height:24px;">Your Name</label><br>&nbsp;&nbsp; 

這裏是我的實際代碼

<div>&nbsp;&nbsp; 
    <label for="name" style="line-height:24px;">Your Name</label><br>&nbsp;&nbsp; 
    <input class="css_form_namefield TooltipIstok " type="text" name="name" value="" style="width: 554px;" maxlength="50" > 
</div> 

回答

20

如果在標籤元素中使用for屬性,它必須與表單中輸入元素的id相匹配。

<label for="field-id" style="line-height:24px;">Your Name</label><br>&nbsp;&nbsp; 
<input type="text" id="field-id"> 

此頁可能是更多的信息很有幫助。 http://www.w3.org/TR/WCAG-TECHS/H44.html

+0

這是我的代碼

  
  
SOoCreative

+4

這個答案是錯誤的。你必須匹配id,而不是名稱。請參閱:http://www.w3.org/TR/html5/forms.html#the-label-element。下面的答案是正確的。 – mtl

10

根據定義,for attribute值必須與「另一個」窗體控件的id屬性值匹配才能使用HTML 4.01術語。 Controlsinput,textarea,button,selectobject元素創建,因此只需將「another」作爲「a」。 HTML5對此有些不同,指定該屬性必須引用labelable element

從錯誤信息,似乎要驗證對HTML5,所以適用的規則是for屬性必須指向一個buttoninputkeygenmeteroutputprogress(比type=hidden等), selecttextarea元素。我的猜測是,您只是忘記了id屬性,錯誤地認爲name屬性可以完成其工作。

相關問題