Bootstrap 3菜单 - 两个折叠按钮 - 一次只打开一个

时间:2014-06-21 04:51:56

标签: javascript jquery html css twitter-bootstrap

我正在尝试在bootstrap 3中创建一个菜单。当它达到移动尺寸时,我有两个折叠导航切换按钮。默认情况下,当一个打开时,它只是堆叠在一起。我想要的是一次打开一个。

    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <!-- 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>
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-search">
        <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="#">Brand</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <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>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
    <div id="test2" class="nav-search collapse">
            <div class="search_box">
            <ul class="nav navbar-nav">
                <li><a href="#">message 1</a></li>
                <li><a href="#">message 1</a></li>
                <li><a href="#">message 1</a></li>
                <li><a href="#">message 1</a></li>
            </ul>
            </div><!-- end search_box -->
      </div><!--/.nav-search -->
  </div><!-- /.container-fluid -->
</nav>

我搜索并发现了这个,但这是针对bootstrap 2 Bootstrap Menu - Two Collapse buttons which toggle each other? Meaning, only 1 open at a time

我觉得我很遗憾。以下是jsfiddle链接: http://jsfiddle.net/Bootstrap714/RfsS9/4/

2 个答案:

答案 0 :(得分:7)

您的活动名称错误。您需要在on('show.bs.collapse'...而不是on('show'...上调用它。

Here is a fixed fiddle

答案 1 :(得分:0)

发现了一种&#34; hackish&#34;溶液

如果兄弟姐妹按钮没有折叠课程,请点击它。

$('.navbar-toggle').on('click', function () {
    if(!$(this).siblings('.navbar-toggle').hasClass('collapsed')) {
       $(this).siblings('.navbar-toggle').click();
       }
});

http://jsfiddle.net/t96Ry/