我应该将输入元素放在标签元素中吗? [英] Should I put input elements inside a label element?
本文介绍了我应该将输入元素放在标签元素中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有关于 label
和 input
HTML 元素的嵌套的最佳实践?
Is there a best practice concerning the nesting of label
and input
HTML elements?
经典方式:
<label for="myinput">My Text</label>
<input type="text" id="myinput" />
或
<label for="myinput">My Text
<input type="text" id="myinput" />
</label>
推荐答案
来自 W3 的 HTML4 规范:
标签本身可能位于之前、之后或周围关联控件.
The label itself may be positioned before, after or around the associated control.
<label for="lastname">Last Name</label>
<input type="text" id="lastname" />
或
<input type="text" id="lastname" />
<label for="lastname">Last Name</label>
或
<label>
<input type="text" name="lastname" />
Last Name
</label>
请注意,当表格用于布局时,不能使用第三种技术,标签在一个单元格中,其关联的表单字段在另一个单元格中.
Note that the third technique cannot be used when a table is being used for layout, with the label in one cell and its associated form field in another cell.
任何一个都有效.我喜欢使用第一个或第二个示例,因为它可以为您提供更多的样式控制.
Either one is valid. I like to use either the first or second example, as it gives you more style control.
这篇关于我应该将输入元素放在标签元素中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文