“为”什么? 在标签标签中?
刚刚在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
“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
在某些浏览器中,当您单击 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
来自 w3schools.org:
哈!
将我的 $.02 添加为可访问性 SME - 除了可用性之外,标签还将输入字段与正确的标签相关联,以便使用屏幕阅读器的人知道该字段的用途。
From w3schools.org:
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.
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.
它指定标签绑定到哪个元素。 在您的示例代码中,标签位于 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.
它将标签与表单元素 id 联系起来。 某些表单元素(例如复选框)可以通过单击其标签来激活。
It ties the label to a form element id. Some form elements, like checkboxes, can be activated by clicking on their label.