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

前端 未结 13 1229
刺人心
刺人心 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();
        });
    });
    
    0 讨论(0)
  • 2020-11-30 05:50

    I had the same issue before. Try this answer here, it works on my site.

    $(function(){
      var current_page_URL = location.href;
      $( "a" ).each(function() {
         if ($(this).attr("href") !== "#") {
           var target_URL = $(this).prop("href");
           if (target_URL == current_page_URL) {
              $('nav a').parents('li, ul').removeClass('active');
              $(this).parent('li').addClass('active');
              return false;
           }
         }
      });
    });
    

    Source: It was original posted here how to set active class to nav menu from twitter bootstrap

    0 讨论(0)
  • 2020-11-30 05:51

    This will do the trick

     $(document).ready(function () {
            var url = window.location;
            var element = $('ul.nav a').filter(function() {
                return this.href == url || url.href.indexOf(this.href) == 0;
            }).addClass('active').parent().parent().addClass('in').parent();
            if (element.is('li')) {
                element.addClass('active');
            }
        });
    
    0 讨论(0)
  • 2020-11-30 05:51

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    $(document).ready(function () {
            var url = window.location;
            $('ul.nav a[href="' + url + '"]').parent().addClass('active');
            $('ul.nav a').filter(function () {
                return this.href == url;
            }).parent().addClass('active').parent().parent().addClass('active');
        });
        
        This works perfectly

    0 讨论(0)
  • 2020-11-30 05:52

    You are binding you click on the wrong element, you should bind it to the a.

    You are prevent default event to occur on the li, but li have no default behavior, a does.

    Try this:

    $(document).ready(function () {
        $('.nav li a').click(function(e) {
    
            $('.nav li.active').removeClass('active');
    
            var $parent = $(this).parent();
            $parent.addClass('active');
            e.preventDefault();
        });
    });
    
    0 讨论(0)
  • 2020-11-30 05:53

    guys try this is a perfect answer for this question:

    <script>
        $(function(){
            $('.nav li a').filter(function(){return this.href==location.href}).parent().addClass('active').siblings().removeClass('active')
            $('.nav li a').click(function(){
                $(this).parent().addClass('active').siblings().removeClass('active')    
            })
        })
        </script>

    0 讨论(0)
提交回复
热议问题