Should I put input elements inside a label element?

前端 未结 14 1981
长情又很酷
长情又很酷 2020-11-22 07:24

Is there a best practice concerning the nesting of label and input HTML elements?

classic way:



        
14条回答
  •  [愿得一人]
    2020-11-22 08:03

    See http://www.w3.org/TR/html401/interact/forms.html#h-17.9 for the W3 recommendations.

    They say it can be done either way. They describe the two methods as explicit (using "for" with the element's id) and implicit (embedding the element in the label):

    Explicit:

    The for attribute associates a label with another control explicitly: the value of the for attribute must be the same as the value of the id attribute of the associated control element.

    Implicit:

    To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element.

提交回复
热议问题