验证 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:40:32  来源:igfitidea点击:

validating HTML: The for attribute of the label element must refer to a form control

htmlvalidating

提问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>&nbsp;&nbsp;

here is my actual code

这是我的实际代码

<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>

回答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>&nbsp;&nbsp;
<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 forattributevalue must match the idattribute value of “another” form control, to use the HTML 4.01 terminology. Controlsare created by input, textarea, button, select, or objectelements, 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 术语。控件inputtextareabuttonselectobject元素创建,因此只需将“another”读作“a”。HTML5 对此有些不同,指定该属性必须引用一个标记元素

From the error message, it seems that you are validating against HTML5, so the rule that applies is that the forattribute must refer to a button, input(other than with type=hidden), keygen, meter, output, progress, select, or textareaelement. My guess is that you just forgot the idattribute, incorrectly assuming that the nameattribute could do its job.

从错误消息来看,您似乎正在针对 HTML5 进行验证,因此适用的规则是该for属性必须引用button, input(与type=hidden除外)keygenmeteroutputprogressselect、 或textarea元素。我的猜测是您只是忘记了该id属性,错误地假设该name属性可以完成其工作。