How to prevent a link inside a label from triggering a checkbox?

前端 未结 8 1465
难免孤独
难免孤独 2021-01-03 18:41

I have this label:

相关标签:
8条回答
  • 2021-01-03 19:28

    Interactive elements inside interactive elements cause functional problems like this: it is undefined what happens when you click an a element inside a label element or vice versa. To avoid this, take the a element out of the label element, e.g.

    <label><input type="checkbox">I agree</label> to the <a href="terms">terms</a>
    and would like to continue
    

    or

    <input type="checkbox" id="agree"><label for="agree">I agree</label> to the
    <a href="terms">terms</a> and would like to continue
    
    0 讨论(0)
  • 2021-01-03 19:29

    Stop propagation of the click event on the link. This prevents the click event from bubbling up to the label.

    https://developer.mozilla.org/en-US/docs/Web/API/event.stopPropagation

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