是“为了”如果目标输入嵌套在标签内,HTML 标签中是否需要属性?

发布于 2024-12-24 19:07:45 字数 177 浏览 1 评论 0原文

我注意到,当您将输入元素放入标签元素时,HTML 标签标记不需要“for”属性:

<label><input type="text">Last name</label>

但我想知道最佳实践是什么。有人可以帮我吗?

谢谢!

I've noticed that a HTML label tag doesn't need the 'for' attribute when you put your input element into the label element:

<label><input type="text">Last name</label>

But I was wondering what's the best practise. Can anybody help me with that?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二货你真萌 2024-12-31 19:07:45

它用于屏幕阅读器等的辅助功能,即

use_the_label_element_to_make_your_html_forms_accessible

所以你应该使用它。这里有一个链接,可以让您了解可访问性的重要性。

这里有一个小故事 - 让您的网站无障碍可以让所有用户受益 - 我一直对市政当局为轮椅无障碍所付出的努力感到惊讶,直到我有了一个女儿并使用婴儿车。我认为网站遵循同样的规则——每个人都受益。

为争论道歉

It's used for accessibility for screen readers and the like i.e.

use_the_label_element_to_make_your_html_forms_accessible

So you should use it. And here is a link to convince you about the importance of accessibily.

And here is a little story - making your site accessible can benefit all users - i always was amazed at the amount of effort civic authorities went to for wheelchair accessibilty until I had a daughter and use a push chair. I think websites follow the same rule - everyone benefits.

Apologies for the polemic

魔法唧唧 2024-12-31 19:07:45

W3 HTML 5.2 标准WhatWG Living Standard 状态(几乎准确地说,引用来自后者):

可以指定 for 属性来指示与标题相关联的表单控件。如果指定了该属性,则该属性的值必须是与标签元素位于同一树中的可标签元素的 ID。如果指定了该属性,并且树中有一个元素的 ID 等于 for 属性的值,并且树顺序中的第一个这样的元素是可标记元素,则该元素是 label 元素的标记控件。

因此,就遵循 HTML 标准而言,这样使用是可以的。

Both the W3 HTML 5.2 standard and the WhatWG Living Standard state (in almost exact terms, quote is from the latter):

The for attribute may be specified to indicate a form control with which the caption is to be associated. If the attribute is specified, the attribute's value must be the ID of a labelable element in the same tree as the label element. If the attribute is specified and there is an element in the tree whose ID is equal to the value of the for attribute, and the first such element in tree order is a labelable element, then that element is the label element's labeled control.

So it's okay to use it that way in terms of following the HTML standard.

谎言 2024-12-31 19:07:45

for 属性对于文本输入没有太大区别,但对于复选框输入非常有用,因为它允许用户单击标签以及复选框本身:

<label for="chk">Checkbox</label><input type="checkbox" id="chk" />

The for attribute doesn't make much difference with a text input, but is very useful with a checkbox input, as it allows users to click on the label as well as the checkbox itself:

<label for="chk">Checkbox</label><input type="checkbox" id="chk" />
书间行客 2024-12-31 19:07:45

您可以将 input 包含在您的 label 中,并将其与 label 关联,或者如果由于某种原因您必须拥有您的 label 元素位于 DOM 中的其他位置,您可以使用 for 属性指定其含义。不管怎样,使用 for 属性总是没有坏处的:)

You can include the input in your label and it is associated with the label, or if for some reason you have to have your label element elsewhere in the DOM, you can specify it's meaning with the for attribute. It never hurts to use the forattribute though either way :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文