What's the “for” for in a label tag?

后端 未结 6 2004
臣服心动
臣服心动 2021-01-31 07:10

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


*

        
相关标签:
6条回答
  • 2021-01-31 07:28

    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.

    0 讨论(0)
  • 2021-01-31 07:32

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

    0 讨论(0)
  • 2021-01-31 07:35

    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

    0 讨论(0)
  • 2021-01-31 07:39

    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.

    0 讨论(0)
  • 2021-01-31 07:39

    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.

    0 讨论(0)
  • 2021-01-31 07:47

    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

    0 讨论(0)
提交回复
热议问题