【问题标题】:Bootstrap 4 navbar active class not working [duplicate]Bootstrap 4导航栏活动类不起作用[重复]
【发布时间】:2019-07-01 03:29:53
【问题描述】:

在一个网站中,我试图让这个特定的导航栏活动类工作。当我点击当前页面时,我希望链接变成蓝色。由于我是初学者,任何帮助将不胜感激。提前致谢。

这里是导航栏:

<div class="nav-scroller py-1 mb-2">

   <nav class="nav d-flex justify-content-between">


      <a class="active p-2 text-muted" href="#"><b> Business</b> </a>
      <a class="p-2 text-muted" href="#"><b> Design </b></a>
      <a class="p-2 text-muted" href="#"><b>Travel </b></a>
      <a class="p-2 text-muted" href="#"><b> Opinion </b></a>

   </nav>

</div>

这里是javascript代码:

$('.nav .p-2 .text-muted').click(function(){
$('.nav .p-2 .text-muted').removeClass('active');
$(this).addClass('active');
})

我还使用 CSS 将当前导航栏页面链接变为蓝色:

.nav  > .p-2.activate  {
color:blue;
}

【问题讨论】:

  • 在您的 CSS 中,您将类称为 activate,在您的 HTML 和 jQuery 中,您将其称为 active

标签: javascript html css django bootstrap-4


【解决方案1】:

这是工作演示:

$(document).ready(function(){
 $('nav a').click(function(){
   $('nav a').removeClass('active');
   $(this).addClass('active');
   let link=$(this).attr('href');
   window.location.href=link;
 });
});
.nav >a.active>b{
  color:blue;
}
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">


<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
    
    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    
    
    <div class="nav-scroller py-1 mb-2">

   <nav class="nav d-flex justify-content-between">


      <a class="active p-2 text-muted" href="https://fonts.google.com"><b> Business</b> </a>
      <a class="p-2 text-muted" href="https://facebook.com"><b> Design </b></a>
      <a class="p-2 text-muted" href="google.com"><b>Travel </b></a>
      <a class="p-2 text-muted" href="#"><b> Opinion </b></a>

   </nav>

</div>

  </body>
</html>

你需要将section="yourpagename"从服务器端发送到前端,

现在您可以通过 2 种方式显示活动链接:

1)第一个是做三元条件并在标签中添加类

类似这样:

<a class="p-2 text-muted <% section=="Business"?'active':'' %>" href="#"><b> Business</b> </a>

2) 第二种方法是使用 jQuery 添加活动类

 <a id="Business" class="p-2 text-muted" href="#"><b> Business</b> </a>

$(document).ready(function(){
    var section= "Business" . <--send value from server side same as link id
    $('#'+section).addClass('active');
});

【讨论】:

  • 感谢这个演示,但是当我让链接为空时它正在工作 (href = "#") 你认为可能是什么问题?
  • @ClaudeVernetMichel ,检查更新的答案
  • @ClaudeVernetMichel,您使用的是哪个后端? php还是nodejs?
  • 它仍然无法正常工作,我使用的是 Django 2.1
  • @ClaudeVernetMichel ,请检查更新的答案
【解决方案2】:

首先你错误选择selector,添加这个

$('.nav .p-2.text-muted').click(function(){
  $('.nav .p-2.text-muted').removeClass('active');
  $(this).addClass('active');
})

还有css

.nav  > .p-2.active  {
color:blue;
}

【讨论】:

    【解决方案3】:

    在您的 JQuery 中,您添加了名为 active 的类,但在 css 中添加了类 activated 。改成active

    .nav > .p-2.active { color:blue; }
    

    【讨论】:

      猜你喜欢
      • 2020-07-17
      • 2018-03-22
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 2015-12-25
      • 2020-01-09
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多