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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24338409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:59:47  来源:igfitidea点击:

Bootstrap 3 menu - two collapse buttons - only one open at a time

javascriptjqueryhtmlcsstwitter-bootstrap

提问by bootstrap714

I am trying to make a menu in bootstrap 3. When it gets down to mobile size, I have two collapse nav-toggle buttons. As default, when one is open, it just gets stacked on top of each other. What I would like is one drop down open at a time.

我正在尝试在引导程序 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>

I searched and found this, but that is for bootstrap 2: Bootstrap Menu - Two Collapse buttons which toggle each other? Meaning, only 1 open at a time

我搜索并找到了这个,但那是针对引导程序 2 的引导程序菜单 - 两个相互切换的折叠按钮?意思是,一次只打开 1 个

I feel like I am missing something easy. Below is a jsfiddle link: http://jsfiddle.net/Bootstrap714/RfsS9/4/

我觉得我错过了一些简单的东西。下面是一个 jsfiddle 链接:http: //jsfiddle.net/Bootstrap714/RfsS9/4/

回答by Andrew

You have got the name of the event wrong. You need to call it on on('show.bs.collapse'...instead of just on on('show'....

你弄错了事件的名称。您需要调用它 onon('show.bs.collapse'...而不仅仅是 on on('show'...

Here is a fixed fiddle

这是一个固定的小提琴

回答by Motti Horesh

Found a kind of "hackish" solution

找到了一种“hackish”解决方案

if, the sibilings button, doesn't have a collapsed class, click on it.

如果同级按钮没有折叠的类,请单击它。

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

http://jsfiddle.net/t96Ry/

http://jsfiddle.net/t96Ry/