当我单击一个时,Bootstrap崩溃只会扩展,其余的将自动隐藏

时间:2017-02-13 08:22:17

标签: jquery twitter-bootstrap

当我点击更多时,它会很好地扩展。但是当我点击另一个时,我想隐藏其余的崩溃。这是我的源代码:



this.form.controls['fromDate'].updateValueAndValidity(new Date(localStorage.getItem("startDate")).getDate().toDateString());
this.form.controls['toDate'].updateValueAndValidity(new Date(localStorage.getItem("endDate")).getDate().toDateString());




1 个答案:

答案 0 :(得分:0)

您需要做的就是添加一个简短的jQuery代码,它利用了Bootstrap的方法和事件:

$('.content').on('show.bs.collapse', function() {
   $(this).siblings().children().collapse('hide');
});

您可以在下面查看一个有效的示例:



$('.content').on('show.bs.collapse', function() {
  $(this).siblings().children().collapse('hide');
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="content">
  <p>content content content content content</p>
  <div class="collapse" id="first">
    <p>hidden content hidden content hidden content hidden content</p>
  </div>
  <button type="button" class="btn" data-toggle="collapse" data-target="#first">More</button>
</div>
<div class="content">
  <p>content content content content content</p>
  <div class="collapse" id="second">
    <p>hidden content hidden content hidden content hidden content</p>
  </div>
  <button type="button" class="btn" data-toggle="collapse" data-target="#second">More</button>
</div>
<div class="content">
  <p>content content content content content</p>
  <div class="collapse" id="third">
    <p>hidden content hidden content hidden content hidden content</p>
  </div>
  <button type="button" class="btn" data-toggle="collapse" data-target="#third">More</button>
</div>
&#13;
&#13;
&#13;