单击其他项目时菜单不会折叠

时间:2018-11-20 11:50:47

标签: bootstrap-4 collapse

我正在尝试在自己的网站上重现此内容: https://www.bootply.com/bootply/WIu8eya2dQ#

代码如下:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
  <div class="list-group panel">
      <a href="#menu1" class="list-group-item" data-toggle="collapse" data-parent="#sidebar">Item 1 <i class="fa fa-caret-down"></i></a>
      <div class="collapse" id="menu1">
          <a href="#menu1sub1" class="list-group-item" data-toggle="collapse">Subitem 1 <i class="fa fa-caret-down"></i></a>
          <div class="collapse" id="menu1sub1">
              <a href="#" class="list-group-item" data-parent="#menu1sub1">Subitem 1 a</a>
              <a href="#" class="list-group-item" data-parent="#menu1sub1">Subitem 2 b</a>
              <a href="#menu1sub1sub1" class="list-group-item" data-toggle="collapse">Subitem 3 c <i class="fa fa-caret-down"></i></a>
              <div class="collapse" id="menu1sub1sub1">
                  <a href="#" class="list-group-item">Subitem 3 c - item 1</a>
                  <a href="#" class="list-group-item">Subitem 3 c - item 2</a>
              </div>
              <a href="#" class="list-group-item" data-parent="#menu1sub1">Subitem 4 d</a>
              <a href="#menu1sub1sub2" class="list-group-item" data-toggle="collapse">Subitem 5 e <i class="fa fa-caret-down"></i></a>
              <div class="collapse" id="menu1sub1sub2">
                  <a href="#" class="list-group-item" data-parent="#menu1sub1sub2">Subitem 5 e.1</a>
                  <a href="#" class="list-group-item" data-parent="#menu1sub1sub2">Subitem 5 e.2</a>
              </div>
          </div>
          <a href="#" class="list-group-item" data-parent="#menu1">Subitem 2</a>
          <a href="#" class="list-group-item" data-parent="#menu1">Subitem 3</a>
      </div>
      <a href="#" class="list-group-item" data-parent="#sidebar">Item 2</a>
      <a href="#menu3" class="list-group-item" data-toggle="collapse" data-parent="#sidebar">Item 3 <i class="fa fa-caret-down"></i></a>
      <div class="collapse" id="menu3">
          <a href="#" class="list-group-item" data-parent="#menu3">3.1</a>
          <a href="#menu3sub2" class="list-group-item" data-toggle="collapse">3.2 <i class="fa fa-caret-down"></i></a>
          <div class="collapse" id="menu3sub2">
              <a href="#" class="list-group-item" data-parent="#menu3sub2">3.2 a</a>
              <a href="#" class="list-group-item" data-parent="#menu3sub2">3.2 b</a>
              <a href="#" class="list-group-item" data-parent="#menu3sub2">3.2 c</a>
          </div>
          <a href="#" class="list-group-item" data-parent="#menu3">3.3</a>
      </div>
      <a href="#" class="list-group-item" data-parent="#sidebar">Item 4</a>
      <a href="#" class="list-group-item" data-parent="#sidebar">Item 5</a>
  </div>
<script src="/jquery/jquery-2.2.4.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

它运作良好,但是由于某些原因,当我消耗一件物品时,已经展开的物品不会折叠。

1 个答案:

答案 0 :(得分:1)

您显示的示例适用于Bootstrap 3,而不适用于Bootstrap 4。

对于Bootstrap 4中的手风琴行为,应该在0元素中设置data-parent,而不要在触发折叠(collapse)的元素中进行设置。

data-toggle="collapse"

https://www.codeply.com/go/ao94agthVY