¥The
Try it
将
¥Associating a
标签文本不仅在视觉上与其相应的文本输入相关联,而且在视觉上也与其相应的文本输入相关联。它也以编程方式与之关联。这意味着,例如,当用户专注于表单输入时,屏幕阅读器将读出标签,从而使辅助技术用户更容易理解应该输入哪些数据。
当用户单击或触摸/点击标签时,浏览器会将焦点传递到其关联的输入(也会为输入引发生成的事件)。用于集中输入的增加的点击区域为任何试图激活它的人提供了优势 - 包括那些使用触摸屏设备的人。
要将
¥To explicitly associate a
或者,你可以将 直接嵌套在
¥Alternatively, you can nest the directly inside the
html
Do you like peas?
标签所标记的表单控件称为标签元素的标记控件。多个标签可以与同一个表单控件关联:
¥The form control that a label is labeling is called the labeled control of the label element. Multiple labels can be associated with the same form control:
html
可以与
¥Elements that can be associated with a
属性¥Attributes
该元素包括 全局属性。
¥This element includes the global attributes.
for
对于与
注意:要以编程方式设置 for 属性,请使用 htmlFor。
¥Note: To programmatically set the for attribute, use htmlFor.
文档中第一个具有与 for 属性值匹配的 id 属性的元素是该 label 元素的带标签控件 — 如果具有该 id 的元素实际上是 可标记元素。如果它不是可标记元素,则 for 属性无效。如果文档后面还有其他元素也与 id 值匹配,则不会考虑它们。
多个 label 元素可以为其 for 属性赋予相同的值;这样做会导致关联的表单控件(for 值引用的表单控件)具有多个标签。
注意:
¥Note: A
使用 CSS 设计样式¥Styling with CSS
¥There are no special styling considerations for
无障碍¥Accessibility
互动内容¥Interactive content
不要将交互元素(例如 anchors 或 buttons)放置在 label 中。这样做会使人们很难激活与 label 相关的表单输入。
¥Don't place interactive elements such as anchors or buttons inside a label. Doing so makes it difficult for people to activate the form input associated with the label.
不要这样做:
¥Don't do this:
html
I agree to the Terms and Conditions
更喜欢这个:
¥Prefer this:
html
I agree to the Terms and Conditions
标题¥Headings
将 标题元素 放在
¥Placing heading elements within a
如果 form 或表单的一部分需要标题,请使用放置在