折叠菜单中的Bootstrap nav li元素选择器

时间:2014-10-29 18:54:41

标签: twitter-bootstrap navigation selector

我想在折叠的Bootstrap导航栏中选择导航链接。我希望移动版本的nav元素的颜色不同。我尝试使用navbarCollapse.in执行此操作,但这不起作用。

window.onload = $('.navbar-wrapper a').css('color', 'white');
window.onload = $('#navbarCollapse.in a').css('color', '#36454F');

我的导航栏如下所示:

    <nav role="navigation" class="nav navbar navbar-fixed-top navbar-default navbar-wrapper white-link">
            <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>
            <a href="index.html" class="navbar-brand"><div class="logo logo-color-white"></div>Brand</a>
        </div>
        <!-- Collection of nav links and other content for toggling -->
        <div id="navbarCollapse" class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                                <li class="approach-li"><a href="approach.html" style="color: black;"><i class="fa fa-rocket">&nbsp;&nbsp;Approach</i></a></li>
                                <li class="services-li"><a href="services.html" style="color: black;"><i class="fa fa-tags">&nbsp;&nbsp;Services</i></a></li>
                                <li class="about-li"><a href="about.html" style="color: black;"><i class="fa fa-building-o">&nbsp;&nbsp;About</i></a></li>
                            <li class="qa-li"><a href="qa.html" style="color: black;"><i class="fa fa-question-circle">&nbsp;&nbsp;Q&amp;A</i></a></li>             
                                <li class="navbarpadding">
                                      <form action="contact.html">
                                                <button class="btn navbar-btn">Contact us</button>
                                        </form>
                                </li>
            </ul>
                    </div>
                </div>
    </nav>

1 个答案:

答案 0 :(得分:0)

你必须使用jquery吗?在您的情况下,我建议仅使用css,结合媒体查询仅在移动设备上应用颜色修改。

CSS代码

<style type="text/css">

//Color modification will be applied only for mobile devices
@media (max-width: 992px) 
{    
    .navbar-default .navbar-nav > .active > a,
    .navbar-default .navbar-nav > li > a
    {
        color :Red;    
    }
}

</style>

HTML代码

<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        </button>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link</a></li>
        <li><a href="#">Link</a></li>
        </ul>
    </div>
</div>
</nav>

干杯!