Should I put input elements inside a label element?

前端 未结 14 1987
长情又很酷
长情又很酷 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 07:45

    One thing you need to consider is the interaction of checkbox and radio inputs with javascript.

    Using below structure:

    
    

    When user clicks on "Label text" controlCheckbox() function will be fired once.

    But when input tag is clicked the controlCheckbox() function may be fired twice in some older browsers. That's because both input and label tags trigger onclick event attached to checkbox.

    Then you may have some bugs in your checkboxState.

    I've run into this issue lately on IE11. I'm not sure if modern browsers have troubles with this structure.

提交回复
热议问题