gpt4 book ai didi

html - 将表单字段放在标签标签内是好还是坏?

转载 作者:行者123 更新时间:2023-11-28 00:10:18 25 4
gpt4 key购买 nike

我在很多框架、自动生成的代码 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com