我在很多框架、自动生成的代码 (Zend) 和布局模板中注意到了这种趋势。我所说的方法是这样的:
<label for="someField">
<input id="someField" name="someName" />
</label>
与常规方法相比,这种方法有哪些优点和缺点?为什么人们倾向于采用这种方法?
最佳答案
通过 W3C standards判断似乎没问题 在 HTML4.01 和 HTML5 中:
HTML4:
"To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element. The label itself may be positioned before or after the associated control."
在此示例中,我们隐式地将标签与文本输入控件关联起来:
<form action="..." method="post">
<p>
<label>
First Name
<input type="text" name="firstname">
</label>
</p>
</form>
从 HTML5 开始仍然没问题:
"The label element represents a caption in a user interface. The caption can be associated with a specific form control, known as the label element's labeled control, either using the for attribute, or by putting the form control inside the label element itself. - W3C.
关于html - 将表单字段放在标签标签内是好主意还是坏主意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627050/