Bootstrap 4嵌套折叠“数据父级”不起作用

Posted

技术标签:

【中文标题】Bootstrap 4嵌套折叠“数据父级”不起作用【英文标题】:Bootstrap 4 nested collapse "data-parent" not working 【发布时间】:2018-09-07 07:43:13 【问题描述】:

我想使用collapse data-parent options。我需要传统的手风琴行为:显示可折叠项目时,应关闭指定父项下的所有可折叠元素。但它不起作用。我不知道这是因为我的嵌套折叠还是我做错了什么。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="content-nav">
  <a data-parent="#content-articles" role="button" data-toggle="collapse" aria-expanded="false" aria-controls="content-nav__book_third" href="#content-nav__book_third" class="content-nav__book content-nav__book_third">3</a>
  <a data-parent="#content-articles" role="button" data-toggle="collapse" aria-expanded="false" aria-controls="content-nav__book_second" href="#content-nav__book_second" class="content-nav__book content-nav__book_second">2</a>
  <a data-parent="#content-articles" role="button" data-toggle="collapse" aria-expanded="true" aria-controls="content-nav__book_first" href="#content-nav__book_first" class="content-nav__book content-nav__book_first">1</a>
</div>
<!--.content-nav-->
<div id="content-articles">
  <div id="content-nav__book_third" class="content-article collapse">
    <div id="content-article__info_third-1" class="content-article__info collapse">
      Book 3 article 1
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_third-2" class="content-article__info collapse">
      Book 3 article 2
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_third-3" class="content-article__info collapse">
      Book 3 article 3
    </div>
    <!--.content-article__info-->

    <div class="content-article__plan">
      <a data-parent="#content-nav__book_third" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-1" href="#content-article__info_third-1" class="content-article__plan-link content-article__plan-link_third-1">Link - Book 3 article 1</a>
      <a data-parent="#content-nav__book_third" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-2" href="#content-article__info_third-2" class="content-article__plan-link content-article__plan-link_third-2">Link - Book 3 article 2</a>
      <a data-parent="#content-nav__book_third" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-3" href="#content-article__info_third-3" class="content-article__plan-link content-article__plan-link_third-3">Link - Book 3 article 3</a>
    </div>
    <!--.content-article__plan-->
  </div>
  <!--.content-article-->

  <div id="content-nav__book_second" class="content-article collapse">
    <div id="content-article__info_second-1" class="content-article__info collapse">
      Book 2 article 1
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_second-2" class="content-article__info collapse">
      Book 2 article 2
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_second-3" class="content-article__info collapse">
      Book 2 article 3
    </div>
    <!--.content-article__info-->

    <div class="content-article__plan">
      <a data-parent="#content-nav__book_second" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-1" href="#content-article__info_second-1" class="content-article__plan-link content-article__plan-link_second-1">Link - Book 2 article 1</a>
      <a data-parent="#content-nav__book_second" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-2" href="#content-article__info_second-2" class="content-article__plan-link content-article__plan-link_second-2">Link - Book 2 article 2</a>
      <a data-parent="#content-nav__book_second" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-3" href="#content-article__info_second-3" class="content-article__plan-link content-article__plan-link_second-3">Link - Book 2 article 3</a>
    </div>
    <!--.content-article__plan-->
  </div>
  <!--.content-article-->

  <div id="content-nav__book_first" class="content-article collapse show">
    <div id="content-article__info_first-1" class="content-article__info collapse">
      Book 1 article 1
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_first-2" class="content-article__info collapse">
      Book 1 article 2
    </div>
    <!--.content-article__info-->
    <div id="content-article__info_first-3" class="content-article__info collapse">
      Book 1 article 3
    </div>
    <!--.content-article__info-->

    <div class="content-article__plan">
      <a data-parent="#content-nav__book_first" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-1" href="#content-article__info_first-1" class="content-article__plan-link content-article__plan-link_first-1">Link - Book 1 article 1</a>
      <a data-parent="#content-nav__book_first" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-2" href="#content-article__info_first-2" class="content-article__plan-link content-article__plan-link_first-2">Link - Book 1 article 2</a>
      <a data-parent="#content-nav__book_first" data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-3" href="#content-article__info_first-3" class="content-article__plan-link content-article__plan-link_first-3">Link - Book 1 article 3</a>
    </div>
    <!--.content-article__plan-->
  </div>
  <!--.content-article-->
</div>
<!--#content-articles-->

可能我必须添加一些js。但代码不应该是多余的。

【问题讨论】:

【参考方案1】:

它不起作用,因为 data-parent 属性应该是可折叠 (.collapse) 元素,而不是 data-toggle(触发器)元素...

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

<div class="container">
    <div class="content-nav">
        <a role="button" data-toggle="collapse" aria-expanded="false" aria-controls="content-nav__book_third" href="#content-nav__book_third" class="content-nav__book content-nav__book_third">3</a>
        <a role="button" data-toggle="collapse" aria-expanded="false" aria-controls="content-nav__book_second" href="#content-nav__book_second" class="content-nav__book content-nav__book_second">2</a>
        <a role="button" data-toggle="collapse" aria-expanded="true" aria-controls="content-nav__book_first" href="#content-nav__book_first" class="content-nav__book content-nav__book_first">1</a>
    </div>
    <!--.content-nav-->
    <div id="content-articles">
        <div id="content-nav__book_third" data-parent="#content-articles" class="content-article collapse">
            <div id="content-article__info_third-1" data-parent="#content-nav__book_third" class="content-article__info collapse">
                Book 3 article 1
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_third-2" data-parent="#content-nav__book_third" class="content-article__info collapse">
                Book 3 article 2
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_third-3" data-parent="#content-nav__book_third" class="content-article__info collapse">
                Book 3 article 3
            </div>
            <!--.content-article__info-->

            <div class="content-article__plan">
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-1" href="#content-article__info_third-1" class="content-article__plan-link content-article__plan-link_third-1">Link - Book 3 article 1</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-2" href="#content-article__info_third-2" class="content-article__plan-link content-article__plan-link_third-2">Link - Book 3 article 2</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_third-3" href="#content-article__info_third-3" class="content-article__plan-link content-article__plan-link_third-3">Link - Book 3 article 3</a>
            </div>
            <!--.content-article__plan-->
        </div>
        <!--.content-article-->

        <div id="content-nav__book_second" data-parent="#content-articles" class="content-article collapse">
            <div id="content-article__info_second-1" data-parent="#content-nav__book_second" class="content-article__info collapse">
                Book 2 article 1
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_second-2" data-parent="#content-nav__book_second" class="content-article__info collapse">
                Book 2 article 2
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_second-3" data-parent="#content-nav__book_second" class="content-article__info collapse">
                Book 2 article 3
            </div>
            <!--.content-article__info-->

            <div class="content-article__plan">
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-1" href="#content-article__info_second-1" class="content-article__plan-link content-article__plan-link_second-1">Link - Book 2 article 1</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-2" href="#content-article__info_second-2" class="content-article__plan-link content-article__plan-link_second-2">Link - Book 2 article 2</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_second-3" href="#content-article__info_second-3" class="content-article__plan-link content-article__plan-link_second-3">Link - Book 2 article 3</a>
            </div>
            <!--.content-article__plan-->
        </div>
        <!--.content-article-->

        <div id="content-nav__book_first" data-parent="#content-articles" class="content-article collapse show">
            <div id="content-article__info_first-1" data-parent="#content-nav__book_first" class="content-article__info collapse">
                Book 1 article 1
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_first-2" data-parent="#content-nav__book_first" class="content-article__info collapse">
                Book 1 article 2
            </div>
            <!--.content-article__info-->
            <div id="content-article__info_first-3" data-parent="#content-nav__book_first" class="content-article__info collapse">
                Book 1 article 3
            </div>
            <!--.content-article__info-->

            <div class="content-article__plan">
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-1" href="#content-article__info_first-1" class="content-article__plan-link content-article__plan-link_first-1">Link - Book 1 article 1</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-2" href="#content-article__info_first-2" class="content-article__plan-link content-article__plan-link_first-2">Link - Book 1 article 2</a>
                <a data-toggle="collapse" aria-expanded="false" aria-controls="content-article__info_first-3" href="#content-article__info_first-3" class="content-article__plan-link content-article__plan-link_first-3">Link - Book 1 article 3</a>
            </div>
            <!--.content-article__plan-->
        </div>
        <!--.content-article-->
    </div>
    <!--#content-articles-->
</div>

【讨论】:

以上是关于Bootstrap 4嵌套折叠“数据父级”不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 4展开全部/全部折叠按钮在折叠元素已经显示时不起作用

折叠在导航栏面包屑按钮的 ng-bootstrap 和 Angular 4 应用程序中不起作用

Bootstrap 3折叠手风琴:折叠所有作品,但在保持数据父级的同时不能全部展开

导航栏(折叠)不起作用 Bootstrap 5

Bootstrap 折叠菜单链接在移动设备上不起作用

切换按钮折叠在 Bootstrap 导航栏中不起作用