Bootstrap 3切换菜单

时间:2014-01-26 12:03:30

标签: javascript jquery html css twitter-bootstrap

我在一个页面中使用2个Bootstrap 3导航菜单。当菜单变为响应(切换菜单)时,其中一个在我单击切换按钮时工作而另一个不起作用。 我该如何解决这个问题?

以下是菜单的代码。

菜单-1:

<nav class="navbar" role="navigation">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" 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>
                    <a class="navbar-brand" id="slideTrigger" href="#"><i class="glyphicon glyphicon-chevron-down"></i> <span>More From Intuit</span></a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <form class="navbar-form navbar-left" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                            <i class="glyphicon glyphicon-search"></i>
                        </div>
                    </form>
                    <ul class="nav navbar-nav">
                        <li><a href="#">Security</a></li>
                        <li><a href="#">Cart</a></li>
                        <li><a href="#"><i class="glyphicon glyphicon-earphone"></i> +00 000 000 <i class=" glyphicon glyphicon-info-sign"></i></a></li>
                    </ul>
                </div>
            </nav>

菜单-2:

<div class="navbar" role="navigation">
                    <div class="container">
                        <div class="navbar-header">
                            <button type="button" class="navbar-toggle" data-toggle="collapse2" data-target=".navbar-collapse2">
                                <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="#"><img src="img/logo.png" alt="" ></a>
                        </div>
                        <div class="navbar-collapse collapse navbar-collapse2 collapse2">
                            <ul class="nav navbar-nav left_buttons">
                                <li class="active"><a href="#">Products</a>
                                    <ul>
                                        <li><a href="">DropDown</a></li>
                                        <li><a href="">DropDown</a></li>
                                        <li><a href="">DropDown</a></li>
                                        <li><a href="">DropDown</a></li>
                                        <li><a href="">DropDown</a></li>
                                    </ul>
                                </li>
                                <li><a href="#">Addons</a></li>
                                <li><a href="#">Checks</a></li>
                                <li><a href="#">Support</a></li>
                            </ul>
                            <ul class="nav navbar-nav navbar-right right_buttons">
                                <li><a href="#" class="btn btn-default trial_button">Free Trial</a></li>
                                <li><a href="#" class="btn btn-primary signin_button">Sign In</a></li>
                            </ul>
                        </div>
                    </div>
                </div>

1 个答案:

答案 0 :(得分:0)

尝试在第二个菜单中的data-toggle="collapse"上使用data-toggle="collapse2"代替button.navbar-toggle

因此.navbar-collapse2中不需要.collapse2div.navbar-collapse类。

同时为div.navbar-collapse添加一个ID,并在data-target中将button.navbar-toggle指向此ID,而不是类。即使指向类正常工作,最好使用id,尽可能使它指向本机javascript方法getElementById,而不是jQuery的$('.class')。它使您的代码工作更快一些。