简体   繁体   English

单击后Bootstrap崩溃不崩溃

[英]Bootstrap collapse not collapsing after click

I'm using bootstrap 4 alpha collapse option..I have two menu and i have given the collpase class position:absolute.So that when ever i click on any collapse button it should open it's content and when click the next collapse button it should show it's content .Everything works fine but problem is when i click the GOOD button it showing it's content and the click Bad button it showing it's content in place of Good buttons content but if i again click on the BAD button or Good button again it is not collapsing the content just moving the content one to another. 我正在使用bootstrap 4 alpha折叠选项..我有两个菜单,并且给了collpase类位置:absolute。因此,当我单击任何折叠按钮时,它应该打开它的内容,而当单击下一个折叠按钮时,它应该打开显示它的内容。一切正常,但是问题是当我单击“良好”按钮显示它的内容,然后单击“不良”按钮显示它的内容代替“良好”按钮的内容,但是如果我再次单击“不良”按钮或“良好”按钮,它是不会折叠内容,而只是将内容移动到另一个。

<p>
  <a class="btn btn-danger" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    GOOD
  </a>
  <a class="btn btn-primary" data-toggle="collapse" href="#collapseExamplesecond" aria-expanded="false" aria-controls="collapseExample">
    BAD
  </a>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-block">
   I am good
  </div>
</div>
<div class="collapse" id="collapseExamplesecond">
  <div class="card card-block">
    I am bad
  </div>
</div>


.collapse{
  position:absolute;
  width:100%;
}

I want to use this collapse as click on single button it would show content and again click on that button it should collapse and when click first on Good button then if i click Bad button it should close the good button's content and show the Bad button content vice versa. 我要使用此折叠,因为单击单个按钮将显示内容,然后再次单击该按钮,它应该折叠,并且当第一次单击“良好”按钮时,如果我单击“不良”按钮,则应关闭该良好按钮的内容并显示“不良”按钮的内容反之亦然。

Here is DEMO 这是演示

The problem with provided example is that when using position:absolute will will overlap next element and hence giving weird behavior. 提供的示例的问题在于,当使用position:absolute时,它将与下一个元素重叠,从而产生怪异的行为。

The solution for this problem is that we have to remove position:absolute from CSS and we have to close all collapsible before opening other ones. 解决此问题的方法是,我们必须从CSS中删除position:absolute ,并且必须在打开其他可折叠对象之前关闭所有可折叠对象。

Here is the working example : https://jsfiddle.net/ep2s35cm/ 这是工作示例: https : //jsfiddle.net/ep2s35cm/

HTML 的HTML

<p>
  <a class="btn btn-danger myButton" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    GOOD
  </a>
  <a class="btn btn-primary myButton" data-toggle="collapse" href="#collapseExamplesecond" aria-expanded="false" aria-controls="collapseExample">
    BAD
  </a>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-block">
   I am good
  </div>
</div>
<div class="collapse" id="collapseExamplesecond">
  <div class="card card-block">
    I am bad
  </div>
</div>

CSS 的CSS

.collapse{
  width:100%;
}

JavaScript 的JavaScript

$(document).ready(function(){
 $('.myButton').on('click',function(){
 $('.collapse').collapse('hide');
 })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM