简体   繁体   中英

How to Use JQuery to Make 'nav-link' Highlight When Selected?

I'm wanting to implement JQuery into my Bootstrap project so that when I select a page on the website, it will highlight when on said page. I think I require some feedback from the community at this point. Here is the JQuery I am trying to implement and also my code for the navigation, respectively:

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


<nav class="navbar navbar-expand-md navbar-dark fixed-top">
    <div class="container">
      <a class="navbar-brand" href="index.html">Homeschool Herpetology</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
      </button>
      <div class="navbar-collapse collapse" id="navbarResponsive">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item active">
            <a class="nav-link" href="index.html">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="about.html">About</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link" href="reptiles.html">Our Reptiles</a>
            <div class="dropdown-menu" aria-labeledby="navbarDropdown">
              <a class="dropdown-item" href="#">Little Foot</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="reptiles.html#mangoMedia">Mango</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="reptiles.html#montyMedia">Monty</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="reptiles.html#sheikMedia">Sheik</a>
              <div class="dropdown-divider"></div>
              <a class="dropdown-item" href="reptiles.html#zeldaMedia">Zelda</a>
            </div>  
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Booking</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="contact.html">Contact</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>

Is This what you were thinking of?

$(function(){
        $('a').each(function(){
            if ($(this).prop('href') == window.location.href) {
                $(this).addClass('active'); $(this).parents('li').addClass('active');
            } else 
            { 
              $(this).removeClass('active'); 
              $(this).parents('li').removeClass('active');
            }
        });
    });

Then, you just style your active class in CSS for whatever styling you'd like the highlighted link to have. :)

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