可折叠的bootstrap,显示第一个元素

时间:2013-02-13 11:20:51

标签: twitter-bootstrap collapse bootstrap-accordion

我想展示这款可折叠手风琴的第一个元素。我在互联网上做了一些研究,但徒劳无功

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha256-KXn5puMvxCw+dAYznun+drMdG1IFl3agK0p/pqT9KAo= sha512-2e8qq0ETcfWRI4HJBzQiA3UoyFk6tbNyG+qSaIBZLyW9Xf3sWZHN/lxe9fTh1U45DpPf07yj94KsUHHWe4Yk1A==" crossorigin="anonymous"></script>

<div class="accordion" id="accordion2">
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne" >
        Aller au dossier personnel d'un agent
      </a>
    </div>
    <div id="collapseOne" class="accordion-body collapse" style="height: 0px; ">
      <div class="accordion-inner">
        <form action="goToAgent.do" method="post">
          <p align="center">
            <input type="hidden" value="identite" name="retour">
            <label for="nomPrenom" >
              Entrer le nom complet de l'agent:
            </label>
            <input type="text" name="nomPrenom" class="typeahead" /> <br>
            <input type="submit" value="appliquer" class="btn btn-info" />
        </form>
      </div>
    </div>
  </div>
  <div class="accordion-group">
    <div class="accordion-heading">
      <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
        Ajout d'un nouveau fonctionnaire
      </a>
    </div>
    <div id="collapseTwo" class="accordion-body collapse">
      <div class="accordion-inner">
        <!-- lorem -->
      </div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:8)

我认为你没有掌握手风琴所需的所有HTML,除非你没有将它粘贴在上面。

Bootstrap docs第一个示例将第一个元素设置为打开:

http://twitter.github.com/bootstrap/javascript.html#collapse(这些文档适用于v2.3.2,不再受到官方支持。)

我认为它是“in”中的一个类,它最初是开放的。

<div class="accordion" id="accordion2">
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
Collapsible Group Item #1
</a>
</div>
<div id="collapseOne" class="accordion-body collapse in">
<div class="accordion-inner">
Anim pariatur cliche...
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
Collapsible Group Item #2
</a>
</div>
<div id="collapseTwo" class="accordion-body collapse">
<div class="accordion-inner">
Anim pariatur cliche...
</div>
</div>
</div>
</div>

答案 1 :(得分:4)

你必须看得太远。

如果您将.in课程改为.collapse,则会保持开放状态:

<div class="accordion-group">  
  <div class="accordion-heading">  
    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">  
     Ajout d'un nouveau fonctionnaire
    </a>  
  </div>  
  <div id="collapseTwo" class="accordion-body collapse in"> 
    <div class="accordion-inner">
    </div>  
  </div>  
</div>

Demo (jsfiddle)

<小时/> 你的标记很乱,所以这里是带有正确标记的演示:http://jsfiddle.net/Sherbrow/yYuRz/2/

如果您希望显示style="height: 0px;"元素,则需要从.collapse元素中删除。{/ p>

答案 2 :(得分:2)

您还可以触发第一个折叠元素的点击

$("#accordion a:first").trigger("click");