toggleClass and remove class from all other elements

前端 未结 6 549
感动是毒
感动是毒 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:28

    I'm assuming that you have a class 'size' that contains a number of elements of which only one should be able to have the class 'checked' at any one time.

    $(".size a").click(function()
    {
        if($this).hasClass('checked')
        {
            $(".size a").removeClass('checked');
            $(this).addClass('checked');
        }
        else
        {
            $(this).removeClass('checked');
        }
    }
    

提交回复
热议问题