13

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

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

4

3 回答 3

11

我不认为您已经获得了手风琴工作所需的所有 HTML,除非您没有将其全部粘贴到上面。

Bootstrap 文档的第一个示例将第一个元素设置为打开:

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>
于 2013-02-13T11:25:31.557 回答
10

v4 更新:

.show可以在元素上使用该类.collapse来打开它。

<div class="accordion" id="accordion-parent">
    <a data-toggle="collapse" href="#collapseOne"></a> 
    <div id="collapseOne" class="collapse show" data-parent="#accordion-parent"></div>
    <a data-toggle="collapse" href="#collapseTwo"></a> 
    <div id="collapseTwo" class="collapse" data-parent="#accordion-parent"></div>
</div>

这是使用 v4 的相同示例:Demo v4 (jsfiddle)


引导程序 v2

如果您将.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>

演示(jsfiddle)


这是带有简化标记的演示:http: //jsfiddle.net/Sherbrow/yYuRz/2/

如果要显示它,您确实需要style="height: 0px;"从元素中删除它。.collapse

于 2013-02-13T11:29:54.747 回答
7

您还可以在第一个折叠元素上触发 clic

$("#accordion a:first").trigger("click");
于 2018-02-05T14:24:04.640 回答