Bootstrap崩溃类无法正确切换 [英] Bootstrap collapse class doesn't toggle correctly

查看:64
本文介绍了Bootstrap崩溃类无法正确切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Bootstrap折叠类.我有两个问题.第一种是当我最初单击折叠按钮时,第二次单击才能折叠div.其次,当它确实折叠时,包含在div中的文本仍然可见.我可以说崩溃正在做某事,因为我在外部div周围添加了边框.

I am trying to use the Bootstrap collapse class. I have two issues. The first is when I click my collapse button initially, it takes a second click to collapse the div. Second, when it does collapse the text contained with the div is still visible. I can tell collapse is doing something because I added a border around the outer div.

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

JSFiddle demonstrating the issue: 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>

推荐答案

尝试此代码段.我已经更改了一些课程

Try this snippet. I have changed some classes

h2 {
  margin: 0;
  display: inline-block;
}

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

已编辑

加载并扩展内容

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

Changed log: aria-expanded="true" and adding show class to

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

h2 {
  margin: 0;
  display: inline-block;
}

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

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

For more information you can refer bootstrap 4 documentation. The relevant link is provided below. https://getbootstrap.com/docs/4.0/components/collapse/

这篇关于Bootstrap崩溃类无法正确切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆