Bootstrap折叠类不能正确切换

时间:2018-01-18 16:18:13

标签: javascript jquery html twitter-bootstrap

我正在尝试使用Bootstrap崩溃类。我有两个问题。第一个是我最初点击我的折叠按钮时,需要再次点击才能折叠div。其次,当它崩溃时,div中包含的文本仍然可见。我可以告诉崩溃正在做某事,因为我在外部div附近添加了一个边框。

JSFiddle演示了这个问题:https://jsfiddle.net/sw52dytm/3/

<div style="border: 3px solid #A9A9A9">
  <button data-toggle="collapse"
          data-target="#Info_Collapse"
          class="btn btn-link collapsed">collapse</button>
  <h2>Info</h2>
  <div class="accordion-body collapse show" id="Info_Collapse">
    <span>test</span> 
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

试试这个代码段。我已经改变了一些课程

&#13;
&#13;
h2 {
  margin: 0;
  display: inline-block;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<div style="border: 3px solid #A9A9A9">
  <h2>Info</h2>
  <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    collapse
  </button>

  <div class="collapse" id="collapseExample">

    <span>test</span>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
&#13;
&#13;
&#13;

<强>编辑:

通过展开内容加载

更改日志:aria-expanded="true"并将show class添加到

<div class="collapse show" id="collapseExample">

&#13;
&#13;
h2 {
  margin: 0;
  display: inline-block;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">

<div style="border: 3px solid #A9A9A9">
  <h2>Info</h2>
  <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="true" aria-controls="collapseExample">
        collapse
      </button>

  <div class="collapse show" id="collapseExample">

    <span>test</span>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js" integrity="sha384-a5N7Y/aK3qNeh15eJKGWxsqtnX/wWdSZSKp+81YjTmS15nvnvxKHuzaWwXHDli+4" crossorigin="anonymous"></script>
&#13;
&#13;
&#13;

有关更多信息,请参阅bootstrap 4文档。相关链接如下。 https://getbootstrap.com/docs/4.0/components/collapse/