简体   繁体   English

Bootstrap Navbar Active State Switching Bootstrap(Javascript相关)

[英]Bootstrap Navbar Active State Switching Bootstrap (Javascript Related)

I use the class="active" on my navbar navigation items with the most current version of bootstrap (4) and it does not switch when I click on the links.我使用最新版本的引导程序 (4) 在我的导航栏导航项上使用 class="active" 并且当我单击链接时它不会切换。 I found similar questions to this and tried to make the javascript fit my own code, but it didn't work.我发现了与此类似的问题,并试图使 javascript 适合我自己的代码,但没有奏效。 Note: These links on my navbar are all on the same page.注意:我的导航栏上的这些链接都在同一页面上。 The goal is to make the clicked on nav-item link active so that the user knows what they're currently viewing on the page.目标是使点击的导航项链接处于活动状态,以便用户知道他们当前在页面上查看的内容。

Here is what my javascript code looks like:这是我的 javascript 代码的样子:

 $(".nav .nav-link").on("click", function(){ $(".nav").find(".active").removeClass("active"); $(this).addClass("active"); });

Here is what my html navbar code looks like:这是我的 html 导航栏代码的样子:

 <header> <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <img class="logo" src="images/logo.png" width="55px" align="left"> <a class="navbar-brand" href="index.html">ROWAN AEΠ</a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarCollapse"> <!--Div for Alignment Purposes Below --> <div class="navig-align"> <ul class="navbar-nav mr-auto"> <li class="nav-item"> <a class="nav-link active" href="#HOME">HOME</a> </li> <li class="nav-item"> <a class="nav-link" href="#ABOUT">ABOUT</a> </li> <li class="nav-item"> <a class="nav-link" href="#">BOARD</a> </li> <li class="nav-item"> <a class="nav-link" href="#">RECRUITMENT</a> </li> <li class="nav-item"> <a class="nav-link" href="#">FAQ</a> </li> <li class="nav-item"> <a class="nav-link" href="#">CONTACT</a> </li> <li class="nav-item"> <a id="donatebutton" class="nav-link" href="http://www.example.org" target="_blank">DONATE <img class="donateimg" src="images/externallink.png" alt=""></a> </li> </ul> </div> </div> </nav> </header>

If someone can look at my html and figure out a solution to for the script, I will be very grateful.如果有人可以查看我的 html 并找出脚本的解决方案,我将不胜感激。

You dont appear to have an ancestor to the nav list with the class of "nav" - also the nav-link li's are what should get the 'active' class - not the 'a' within it.您似乎没有具有“nav”类的导航列表的祖先——导航链接 li 也是应该获得“活动”类的东西——而不是其中的“a”。

Also - you can target the active item directly to remove the active class - whether its the li or the a.此外 - 您可以直接定位活动项目以删除活动类 - 无论是 li 还是 a。

if you want to keep the active class on the a - then just change the click handler如果您想在 a 上保留活动类 - 那么只需更改点击处理程序

$(".nav-link").on("click", function(){
   $(".nav-link.active").removeClass("active");
   $(this).addClass("active");
});

If you want the active class on the li - then if you want to keep the active class on the a - then just change the click handler如果您想要 li 上的活动类 - 那么如果您想将活动类保留在 a 上 - 那么只需更改点击处理程序

$(".nav-item").on("click", function(){
   $(".nav-item.active).removeClass("active");
   $(this).addClass("active");
});

You do not have a class .nav which you are referencing in your jQuery selector.您没有在 jQuery 选择器中引用的 .nav 类。

Update your jQuery too:也更新你的 jQuery:

$(".nav-link").on("click", function(){
    $(".nav-link.active").removeClass("active");
    $(this).addClass("active");
});

Link to example: https://jsfiddle.net/Lb3u9c5d/链接到示例: https : //jsfiddle.net/Lb3u9c5d/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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