【问题标题】:Navbar CSS automatic add active in dropdown menu导航栏 CSS 自动添加在下拉菜单中处于活动状态
【发布时间】:2017-03-22 01:40:52
【问题描述】:

我想让我的css 导航栏成为我网站中的include。我在这篇文章中得到了一些答案:Twitter Bootstrap add active class to li,我使用 Jquery 在我的导航栏中自动激活。

这是代码:

<nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
               </div>
            <!-- Collection of nav links and other content for toggling -->
            <div id="navbarCollapse" class="collapse navbar-collapse">
                <ul class="nav navbar-nav">
                    <li ><a href="../content/main.php#home">Home</a></li>
                    <li><a href="../content/news.php">News</a></li>
                    <li><a href="../content/contact.php">Contact</a></li>
                    <li class="dropdown" >
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> Information <span class="caret"></span></a>
                      <ul class="dropdown-menu">
                        <li><a href="../content/info.php">Indoor</a></li>
                        <li><a href="../content/info.php#outdoor">Outdoor</a></li>
                      </ul>
                    </li>
                    <li class="dropdown" >
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Gallery<span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="../content/photo.php">Photo</a></li>
                            <li><a href="../content/video.php">Video</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <script type="text/javascript">
        var url = window.location;
        // Will only work if string in href matches with location
        $('ul.nav a[href="'+ url +'"]').parent().addClass('active');

        // Will also work for relative and absolute hrefs
        $('ul.nav a').filter(function() {
            return this.href == url;
        }).parent().addClass('active');
    </script>

我的问题是当用户在子 url 中时如何使下拉父级自动激活?谢谢

注意:有一个下拉父级(图库部分)的子级没有相同的 url/链接

【问题讨论】:

    标签: jquery html css twitter-bootstrap navbar


    【解决方案1】:
    var url = window.location;
    $('ul.nav li').removeClass('active'); //remove active class from all li
    $('ul.nav a[href="'+ url +'"]').parents('li').addClass('active'); //add active class to current url's parents this will work in dropdown also
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多