简体   繁体   中英

How to mantain active a nav tab using jquery?

I'm using this js to put active a nav tab in my web page:

  $('a[href="' + this.location.pathname + '"]').parents('li,ul').addClass('active');

It works in the same level(secured/buyer_user/inbox/get/new_purchase)of the view, but if I change a location like secured/buyer_user/inbox/get/new_purchase the nav tab get inactive.

I have the following page structure:

<nav class="navbar navbar-default navbar-fixed-top" id="nav" name="nav"> 
    <div class="container-fluid">
        <ul class="nav navbar-nav hover">
           <li>...<li>
        <ul>
    </div>
</nav>

Any help would be great.

Try this

  var loc = window.location.pathname;

   $('#nav').find('a').each(function() {
     $(this).toggleClass('active', $(this).attr('href') == loc);
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM