Clicking on label doesn't trigger click event

前端 未结 3 717
清酒与你
清酒与你 2021-01-11 19:10

I have this code:


and

$(\"label\").click(function () {
  $(         


        
3条回答
  •  轮回少年
    2021-01-11 20:09

    Clicking the label also triggers a click on the input so bind the change event to the checkbox itself:

    $(function(){
        $("label input").on("click",function(){
    	$(this).parent().toggleClass("active");
        });
    });
    .active {color:red;}
    
    

    Or With CSS 3

    If you don't need to support old browsers, you could use the :checked pseudo-class in CSS instead:

    input[type=checkbox]:checked + label {color:red;}
     
     

提交回复
热议问题