toggleClass and remove class from all other elements

前端 未结 6 550
感动是毒
感动是毒 2020-12-01 03:18

How can I toggleClass and remove class from all other elements? Consider a div that contains a tags: html:


      
6条回答
  •  一个人的身影
    2020-12-01 03:29

    This will do it

     $(".size a").click(function(){
        $('.size a.checked').not(this).removeClass('checked');
        $(this).toggleClass('checked');
     })
    

    Update

    Alternatively you could do

     $(".size").on('click','a', function(){
        $(this).toggleClass('checked').siblings().removeClass('checked');
     })
    

提交回复
热议问题