Bootstrap 4 |展开其他部分时折叠

时间:2018-04-04 07:32:47

标签: jquery html5 twitter-bootstrap bootstrap-4 collapse

我正在研究bootstrap 4 Collapse。当一个部分被扩展时,想要折叠其他部分

到目前为止,我做的是:

HTML

<p>
  <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
    content 1
  </a>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample2" aria-expanded="false" aria-controls="collapseExample2">
    Content 2
  </button>
</p>

<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Content one here
  </div>
</div>
<div class="collapse" id="collapseExample2">
  <div class="card card-body">
    Content 2 here
  </div>
</div>

现场演示:Live Demo

2 个答案:

答案 0 :(得分:14)

使用data-parent属性:

<div class="container" id=myGroup>
    <p>
        <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
            content 1
        </a>
        <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample2" aria-expanded="false" aria-controls="collapseExample2">
            Content 2
        </button>
    </p>

    <div class="collapse" id="collapseExample" data-parent="#myGroup">
        <div class="card card-body">
            Content one here
        </div>
    </div>
    <div class="collapse" id="collapseExample2" data-parent="#myGroup">
        <div class="card card-body">
            Content 2 here
        </div>
    </div>
</div>

我已将id添加到您的容器中,并将data-parent添加到您的内容部分,并通过id引用此容器。

答案 1 :(得分:-1)

只需放置data-parent =“#element_parent”,

示例...

    <a class="btn btn-primary" data-toggle="collapse" href="#colla1" role="button" aria-expanded="false" aria-controls="colla1">
        content 1
    </a>
    <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#colla2" aria-expanded="false" aria-controls="colla2">
        Content 2
    </button>
<div class="collapse" id="colla1" data-parent="#element_parent">
    <nav
        your content 1 here
    </nav>
</div>

<div class="collapse" id="colla2" data-parent="#element_parent">
    <nav
        your content 2 here
    </nav>
</div>