twitter-bootstrap 在 Bootstrap 中默认关闭手风琴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27988776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 22:10:28  来源:igfitidea点击:

Make accordion closed by default in Bootstrap

twitter-bootstrapaccordioncollapse

提问by Iqbal hossain

I'm using Bootstrap Accordion. I want all the panel closed by default, but my panels are start expanded.

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

Here's my code:

这是我的代码

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

I also used aria-expanded="false", but it doesn't work.

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

What's the mistake?

有什么错误?

回答by Tim Lewis

This line here seems to be the culprit:

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

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

If you get rid of inin the class, it should work as you have it structured. Take a look at this example to see the difference between the two panels:

如果你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>

Notice the first panel doesn't have the class=collapsedon it's <a>tag, and has the inclass on it's <div>tag, and starts OPEN. The second panel switches those, and starts CLOSED.

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

EDIT

编辑

Can't make a Bootply, keep getting "Application Error".

无法制作 Bootply,不断收到“应用程序错误”。

回答by Venkatesh Thapan

Was recently working with Bootstrap 4 and couldn't actually figure out where the "in" class name was(it is mentioned in multiple answers across similar questions).

最近在使用 Bootstrap 4,但实际上无法弄清楚“in”类名在哪里(在类似问题的多个答案中都提到过)。

After a lot of searching, noticed the updated documentation which says :

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

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

折叠插件利用几个类来处理繁重的工作:

.collapse 隐藏内容

.collapse.show 显示内容

.collapsing 在过渡开始时添加,并在完成时移除

So the example code looks something like this now :

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

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

From which, this is important bit :

从中,这是重要的一点:

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

Removing the "show" class name makes sure the Bootstrap accordion remains collapsed by default.

删除“show”类名可确保 Bootstrap 手风琴在默认情况下保持折叠状态。

Hope this helps!

希望这可以帮助!