add/remove active class for ul list with jquery?

前端 未结 5 876
太阳男子
太阳男子 2020-12-29 07:18

I\'m trying to remove and set an active class for a list item every time it\'s clicked. It\'s currently removing the selected active class but isn\'t setting it. Any idea w

5条回答
  •  北海茫月
    2020-12-29 07:55

    Try this,

     $('.nav-list li').click(function() {
    
        $('.nav-list li.active').removeClass('active');
        $(this).addClass('active');
    });
    

    In your context $(this) will points to the UL element not the Li. Hence you are not getting the expected results.

提交回复
热议问题