在 Bootstrap 中默认关闭手风琴

Make accordion closed by default in Bootstrap

我正在使用 Bootstrap 手风琴。我希望默认情况下关闭所有面板,但我的面板已开始展开。

这是我的代码

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="false">
    <c:forEach items="${proposals}" var="proposal" varStatus="serial">
        <div class="panel panel-default">

            <div class="panel-heading" role="tab" id="heading${proposal.propID}"  >
                <h4 class="panel-title">
                    <span class="fa fa-paperclip" aria-hidden="true"></span>
                    <a class="accordion-toggle collapsed" 
                       data-toggle="collapse" data-parent="#accordion"
                       href="#collapse${proposal.propID}" aria-expanded="false"
                       aria-controls="collapse${proposal.propID}"> 
                           ${proposal.title} 
                    </a>
                </h4>
            </div>

            <div id="collapse${proposal.propID}" class="panel-collapse collapse in"
                 role="tabpanel" aria-labelledby="heading${proposal.propID}">
                ${proposal.interest}
            </div>

        </div>
    </c:forEach> 
</div>

我也用过aria-expanded="false",但是不行。

怎么了?

这里的这一行似乎是罪魁祸首:

<div id="collapse${proposal.propID}" class="panel-collapse collapse in" ...

如果您去掉 class 中的 in,它应该可以按照您的结构运行。看看这个例子,看看两个面板之间的区别:

<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
          This Panel is Open By Default
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
        Open
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingTwo">
      <h4 class="panel-title">
        <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
          This Panel Is Closed By Default
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
      <div class="panel-body">
        Closed
      </div>
    </div>
  </div>
</div>

请注意,第一个面板的 <a> 标签上没有 class=collapsed,而 <div> 标签上有 in class,并开始开放。第二个面板切换那些,并开始关闭。

编辑

无法制作 Bootply,继续 "Application Error"。

最近正在与 Bootstrap 4 合作,但实际上无法弄清楚“in”class 名称在哪里(类似问题的多个答案中都提到了它)。

经过大量搜索后,注意到更新后的文档说:

The collapse plugin utilizes a few classes to handle the heavy lifting:

.collapse hides the content

.collapse.show shows the content

.collapsing is added when the transition starts, and removed when it finishes

所以示例代码现在看起来像这样:

  <div id="accordion">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>

从中,这是重要的一点:

<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">

删除“show”class 名称可确保 Bootstrap 折叠面板默认保持折叠状态。

希望对您有所帮助!

只需要去掉body内容class中的show,默认关闭

<div class="accordion" id="accordionExample">
                  <div class="accordion-item">
                    <h2 class="accordion-header" id="headingOne">
                      <button class="accordion-button collapsed bg-light" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
                        <span class="fw-bold">All Permissions</span><span class="text-notch ms-2">Click to expand</span>
                      </button>
                    </h2>
                    <div id="collapseOne" class="accordion-collapse collapse" aria-labelledby="headingOne" data-bs-parent="#accordionExample">
                      <div class="accordion-body">
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                        <button type="button" class="btn btn-outline-primary mb-3 ms-3">Permission</button>
                      </div>
                    </div>
                  </div>
                </div>