将活动类添加到导航栏

时间:2016-06-22 23:13:32

标签: javascript

我对此有点新意,所以请原谅我问,但是我正在使用一个稍微修改过的bootstrap导航栏,并且最近已经正确分配了活动类,但由于某种原因它停止使用最后一个我做的改变......(试图恢复它看看是什么搞砸了,无济于事......) 一些温柔的灵魂能否启发我如何使其发挥作用呢?

干杯! T.F。

    view:

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">ACAC</a>
            </div>


            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="<?php echo base_url() ?>">INÍCIO<span class="sr-only">(current)</span></a></li>
                    <li><a href="<?php echo base_url() ?>noticias">NOTÍCIAS</a></li>
                    <li><a href="<?php echo base_url() ?>projecto">PROJECTO</a></li>
                    <li><a href="<?php echo base_url() ?>actividades">ACTIVIDADES</a></li>
                    <li><a href="<?php echo base_url() ?>galeria">GALERIA</a></li>
                    <li><a href="<?php echo base_url() ?>contactos">CONTACTOS</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <a href="#" class="lnklang">EN</a>

                </ul>
            </div>
        </div>
    </nav>
    .....
        <script>
    $(".nav a").on("click", function(){
       $(".nav").find(".active").removeClass("active");
       $(this).parent().addClass("active");
    });
    </script>

2 个答案:

答案 0 :(得分:0)

这是删除了hrefs并更改li内容的代码 我的编辑没有改变你的代码如何工作,它似乎工作正常

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">ACAC</a>
    </div>


    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        
        <li class="active"><a>One</a></li>
        <li><a>Two</a></li>
        <li><a>Three</a></li>
        <li><a>Four</a></li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <a href="#" class="lnklang">EN</a>

      </ul>
    </div>
  </div>
</nav>
.....
<script>
  $(".nav a").on("click", function() {
    $(".nav").find(".active").removeClass("active");
    $(this).parent().addClass("active");
  });
</script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

答案 1 :(得分:0)

单击菜单页面导航到新页面。活动菜单已更改,但当页面重新加载到新页面时,它将重置为默认值。

您可以检查document.ready上的页面并设置该页面的活动菜单 -

if (window.location.href.indexOf("noticias") > -1) {
     $(".nav").find(".active").removeClass("active");
     $('.nav a[href$="http://www.base_url.com/notice"]').parent().addClass("active");
}

http://www.base_url.com/更改为您的实际基本网址。