“为”什么? 在标签标签中?

发布于 2024-07-16 06:23:45 字数 350 浏览 13 评论 0原文

刚刚在 HTML 标签标记中遇到了 for 参数:

<label for="required-firstname"> First Name </label>
<small>*</small>
<input name="required-firstname" type="text" tabindex="2" 
       id="required-firstname" size="24" maxlength="40">

我正在将此表单转换为 PHP 处理的脚本,我可以去掉 for= 参数吗? (出于好奇,它有什么作用?)

Just ran across a for parameter in an HTML label tag:

<label for="required-firstname"> First Name </label>
<small>*</small>
<input name="required-firstname" type="text" tabindex="2" 
       id="required-firstname" size="24" maxlength="40">

I'm converting this form to a PHP processed script, can I get rid of the for= parameters?
(And out of curiosity, what does it do?)

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

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

发布评论

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

评论(6

浮光之海 2024-07-23 06:23:46

“for”属性是表单可访问性的必要元素。 不要省略它。 对于使用屏幕阅读器 (SR) 向他们宣布网页的人来说,“for”属性将控件与标签相关联。 通常,SR 用户将通过选项卡浏览表单,从一个控件(这是 SR 的可聚焦元素)到下一个控件。 如果没有“for”属性,SR 用户将必须更改 SR 上的模式并探测表单以尝试确定哪个控件与哪个标签匹配,这可能非常耗时且令人困惑。 “for”属性对于与运动问题相关的辅助技术也很有用。

WebAIM.org 有一个很棒的页面,解释了“for”的可访问性后果:http://webaim.org/techniques /表单/控件

The "for" attribute is a necessary element for the accessibility of your form. Do not omit it. For someone using a screen reader (SR) to have a web page announced to them, the "for" attribute relates the control to the label. Typically a SR user will tab through a form, from one control (which is a focusable element for the SR) to the next. Without the "for" attribute, the SR user will have to change modes on the SR and probe around the form to try and determine which control matches which label, which can be time-consuming and confusing. The "for" attribute can also be useful for assistive technology relating to motor issues.

WebAIM.org has a great page explaining the accessibility ramifications of "for": http://webaim.org/techniques/forms/controls

我的黑色迷你裙 2024-07-23 06:23:46

在某些浏览器中,当您单击 for 标记中的文本时,您将选中与其关联的框(即 for = id)或将焦点放在该框上。 这是 ADA 的事情

In some browsers when you click on a text in a for tag, you'll check the box it's associated with (i.e. the for = id) or put the focus on that box. It's an ADA thing

儭儭莪哋寶赑 2024-07-23 06:23:45

来自 w3schools.org:

标签定义输入元素的标签。

标签元素不会呈现为对用户而言特殊的东西。 但是,它为鼠标用户提供了可用性改进,因为如果用户单击标签元素内的文本,则会切换控件。

标签的 for 属性应该等于相关元素的 id 属性,以将它们绑定在一起。

哈!

将我的 $.02 添加为可访问性 SME - 除了可用性之外,标签还将输入字段与正确的标签相关联,以便使用屏幕阅读器的人知道该字段的用途。

From w3schools.org:

The tag defines a label for an input element.

The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.

The for attribute of the tag should be equal to the id attribute of the related element to bind them together.

HTH!

adding my $.02 as an Accessibility SME - as well as usability, the LABEL also associates the input field with the correct label so persons using screen readers will know what the field is for.

隔岸观火 2024-07-23 06:23:45

HTML label 标签定义表单元素的标签。 它们通常与复选框和单选按钮一起使用,当用户单击标签时,它会切换按钮。 对于文本输入(您必须检查这一点以确保),我认为它只会在用户单击标签时将焦点集中到输入上。

The HTML label tag defines a label for a form element. They're usually used with checkboxes and radio buttons, and when the user clicks on the label it toggles the button. With a text input (and you'll have to check this to be sure) I think it only gives focus to the input when the user clicks the label.

蓝咒 2024-07-23 06:23:45

它指定标签绑定到哪个元素。 在您的示例代码中,标签位于 required-firstname 输入字段中。 如果用户单击该标签,焦点将转到绑定的输入字段。 这是可用性的改进,我认为您最好保持原样。 这是一个很好的做法。

It specifies to which element that label is bound to. In your sample code the label is there for the required-firstname input field. If the user clicks on that label, the focus will go to the bound input field. It's a usability improvement and I think you'd be better off leaving it as is. It's a good practice.

再见回来 2024-07-23 06:23:45

它将标签与表单元素 id 联系起来。 某些表单元素(例如复选框)可以通过单击其标签来激活。

It ties the label to a form element id. Some form elements, like checkboxes, can be activated by clicking on their label.

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