Bootstrap 4:如何创建一个带有手风琴的下拉菜单?

时间:2019-01-29 08:39:38

标签: jquery css bootstrap-4 accordion collapse

我实际上在使自举式手风琴在下拉菜单中工作时遇到一些问题。 我在Google上搜索了很多,还在这里找到了一些StackOverflow线程,但是它们已经存在了几年,并且取决于引导程序3或引导程序2。 像这里这样:Twitter Bootstrap: How to create a dropdown button with an accordion inside it?

任何人都可以在这里帮助我,或者在Bootstrap下拉菜单中找到适用于手风琴的解决方案。

Error:"Network Error"

2 个答案:

答案 0 :(得分:0)

尝试

// Prevent dropdown to be closed when we click on an accordion link
$('.dropdown-accordion').on('click', 'a[data-toggle="collapse"]', function (event) {
  $($(this).attr('href')).collapse('toggle');
  event.stopPropagation();
});
<script src="http://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">                                        
  <div class="dropdown dropdown-accordion">
    <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
      Dropdown
    </button>
 
    <div id="accordion" class="dropdown-menu">
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="card-link" data-toggle="collapse" href="#collapseOne">
           Item #1
        </a>
      </div>
      <div id="collapseOne" class="collapse show" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseTwo">
        Item #2
      </a>
      </div>
      <div id="collapseTwo" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
    <div class="card dropdown-item">
      <div class="card-header">
        <a class="collapsed card-link" data-toggle="collapse" href="#collapseThree">
           Item #3
        </a>
      </div>
      <div id="collapseThree" class="collapse" data-parent="#accordion">
        <div class="card-body">
          Lorem ipsum dolor sit amet
        </div>
      </div>
    </div>
  </div>
  </div>
</div>

答案 1 :(得分:0)

在单击时使导航栏下拉菜单保持打开状态的技巧是使用<form>标签。因此,如果您将手风琴放在<form>内,它将按需要工作。

  <div class="collapse navbar-collapse" id="navbarContent">
        <ul class="navbar-nav">
            <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown
                    </a>
                <div class="dropdown-menu">
                    <form class="accordion" id="accordionExample">
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                                Item #1
                            </a>
                            <div id="collapseOne" class="collapse px-3 show" data-parent="#accordionExample">
                                Collapse 1 content
                            </div>
                        </div>
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                Item #2
                            </a>
                            <div id="collapseTwo" class="collapse px-3" data-parent="#accordionExample">
                                Collapse 2 content...
                            </div>
                        </div>
                        <div>
                            <a class="nav-link" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
                                Item #3
                            </a>
                            <div id="collapseThree" class="collapse px-3" data-parent="#accordionExample">
                                Collapse 3 content...
                            </div>
                        </div>
                    </form>
                </div>
            </li>
        </ul>
    </div>

演示:https://www.codeply.com/go/F8iQy0MeLa