验证 HTML:标签元素的 for 属性必须引用表单控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18945774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validating HTML: The for attribute of the label element must refer to a form control
提问by SOoCreative
I don't know why I keep getting this error while checking my page at http://validator.w3.org/checkThe error was:
我不知道为什么在http://validator.w3.org/check 上检查我的页面时总是收到此错误错误是:
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>
here is my actual code
这是我的实际代码
<div>
<label for="name" style="line-height:24px;">Your Name</label><br>
<input class="css_form_namefield TooltipIstok " type="text" name="name" value="" style="width: 554px;" maxlength="50" >
</div>
回答by Shane A. Darr
If you use the for attribute in a label element it has to match the id of an input element in your form.
如果在 label 元素中使用 for 属性,它必须与表单中输入元素的 id 匹配。
ie
IE
<label for="field-id" style="line-height:24px;">Your Name</label><br>
<input type="text" id="field-id">
This page may be helpful for more information. http://www.w3.org/TR/WCAG-TECHS/H44.html
此页面可能有助于获取更多信息。http://www.w3.org/TR/WCAG-TECHS/H44.html
回答by Jukka K. Korpela
By definition, the for
attributevalue must match the id
attribute value of “another” form control, to use the HTML 4.01 terminology. Controlsare created by input
, textarea
, button
, select
, or object
elements, so just read “another” as “a”. HTML5 puts this somewhat differently, specifying that the attribute must refer to a labelable element.
根据定义,for
属性值必须与id
“另一个”表单控件的属性值匹配,使用 HTML 4.01 术语。控件由input
、textarea
、button
、select
或object
元素创建,因此只需将“another”读作“a”。HTML5 对此有些不同,指定该属性必须引用一个可标记元素。
From the error message, it seems that you are validating against HTML5, so the rule that applies is that the for
attribute must refer to a button
, input
(other than with type=hidden
), keygen
, meter
, output
, progress
, select
, or textarea
element. My guess is that you just forgot the id
attribute, incorrectly assuming that the name
attribute could do its job.
从错误消息来看,您似乎正在针对 HTML5 进行验证,因此适用的规则是该for
属性必须引用button
, input
(与type=hidden
除外)keygen
、meter
、output
、progress
、select
、 或textarea
元素。我的猜测是您只是忘记了该id
属性,错误地假设该name
属性可以完成其工作。