How to change active class while click to another link in bootstrap use jquery?

前端 未结 13 1233
刺人心
刺人心 2020-11-30 05:32

I have a html as sidebar, and use Bootstrap.

13条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 05:45

    If you change the classes and load the content within the same function you should be fine.

    $(document).ready(function(){
        $('.nav li').click(function(event){
            //remove all pre-existing active classes
            $('.active').removeClass('active');
    
            //add the active class to the link we clicked
            $(this).addClass('active');
    
            //Load the content
            //e.g.
            //load the page that the link was pointing to
            //$('#content').load($(this).find(a).attr('href'));      
    
            event.preventDefault();
        });
    });
    

提交回复
热议问题