Add & remove active class from a navigation link

前端 未结 4 489
有刺的猬
有刺的猬 2020-12-18 03:50

I have been looking for tutorials on how to add and remove a class from a link unfortunately without any success. All the earlier questions on this have give me some underst

4条回答
  •  温柔的废话
    2020-12-18 04:46

    You're removing the 'active' class from the closest li's child element, and then you're adding the active class to the current a's parent li. In the spirit of keeping the active class on the anchors and not the list items, this will work for you:

        $('li a').click(function(e) {
            e.preventDefault();
            $('a').removeClass('active');
            $(this).addClass('active');
        });
    

    The active link is the active link. There'd never be more than one link active at any given time, so there's no reason to be all specific about removing the active class. Just remove from all anchors.

    Demo: http://jsfiddle.net/rq9UB/

提交回复
热议问题