在Bootstrap中默认关闭折叠面板

kx1ctssn  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(3)|浏览(324)

我正在使用Bootstrap Accordion。我希望所有面板默认关闭,但我的面板开始展开。

这是我的代码

<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",但它不起作用。
有什么错误吗?

kcrjzv8t

kcrjzv8t1#

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

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

如果你去掉了类中的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类,并启动OPEN。第二个面板切换这些标签,并启动CLOSED。

编辑

无法进行Bootply,不断出现“应用程序错误”。

iqxoj9l9

iqxoj9l92#

我最近在使用Bootstrap 4,但实际上无法找出“in”类名在哪里(在类似问题的多个答案中提到了它)。
经过大量搜索后,注意到更新的文档如下所示:
collapse插件使用几个类来处理繁重的工作:
.collapse隐藏内容
. collapse.show显示的内容
.在转场开始时添加折叠,在转场结束时删除
因此,示例代码现在看起来如下所示:

<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”类名可确保 Bootstrap 折叠面板在默认情况下保持折叠状态。
希望这对你有帮助!

xn1cxnb4

xn1cxnb43#

你所需要做的就是删除show类中的body内容,它将默认关闭。

<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>

相关问题