Bootstrap 可折叠引导,显示第一个元素

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

我想展示这个可折叠 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>
shyt4zoc

shyt4zoc1#

我不认为你已经得到了所有的HTML,你需要的 accordion 工作,除非你还没有粘贴它以上。
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>
icnyk63a

icnyk63a2#

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

Demo (jsfiddle)
以下是使用简化标记的演示:http://jsfiddle.net/Sherbrow/yYuRz/2/
如果要显示style="height: 0px;",则需要从.collapse元素中删除它。

owfi6suc

owfi6suc3#

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

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

相关问题