问题
I've implemented Bootstrap 3 Collapse. The client wants all of the target blocks to expand when any one of the heading links is clicked. I have tried to implement a modified version of this answer, but can't get it working.
How can I get all target blocks to expand/collapse when any one of them is clicked?
This is the markup:
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h6 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapse{entry_id}">{title}</a></h6>
</div>
</div>
<div id="collapse{entry_id}" class="panel-collapse collapse">
<div class="panel-body">
{technology_body}
</div>
</div>
</div>
And this is the JS I have attempted:
$('#accordion').on('click', function() {
if($('.collapse:not(.in)')) {
$.collapse("toggle");
}
});
回答1:
I got some help offline on this question. The script to use is
$('#accordion .panel-default').on('click', function () {
$('#accordion .panel-collapse').collapse('toggle');
});
and this is the JSFiddle example http://jsfiddle.net/gtowle/Vq6gt/1/
回答2:
Figured it out. when you want to collapse your elements - you need to specify the content div of your #accordion, in your case (and also mine) the class is .collapse.
$('#accordion .collapse').collapse('show');
回答3:
I needed to have collapse/expand links to click. Here is my solution:
<a id="acollapse" href="javascript:jQuery('div .panel-collapse').collapse('hide'); jQuery('#acollapse').hide();jQuery('#aexpand').show();">Collapse All</a>';
<a id="aexpand" href="javascript:jQuery('div .panel-collapse').collapse('show'); jQuery('#aexpand').hide(); jQuery('#acollapse').show();" style="display: none;">Expand All</a>
回答4:
Try this it will help you.
$('.panel-collapse').removeData('bs.collapse')
.collapse({parent:false, toggle:false})
.collapse('show')
.removeData('bs.collapse')
.collapse({parent:'#accordion', toggle:false});
回答5:
We have a way to solve this.
<div class="AccordionStyle panel-group" id="Security_accordion">
<div class="ShowAllTabs"><a>SHOW ALL <i class="fa fa-chevron-down"></i></a></div>
<!--= collapse-start =-->
<div class="panel">
<div class="panel-heading">
<div href="#SecurityServices" data-toggle="collapse" data-parent="#Security_accordion" class="panel-title expand collapsed">
<div class="right-arrow pull-right icon_wrap"><i class="fa fa-chevron-down"></i></div>
<a>Security Services</a>
</div>
</div>
<div id="SecurityServices" class="panel-collapse collapse">
<div class="panel-body">
contents will go here...
</div>
</div>
</div>
<!--= END! =-->
</div>
<script>
$('.panel-group .ShowAllTabs').click(function(){
$(this).toggleClass('show_all_panels');
if($(this).hasClass('show_all_panels'))
{
$(this).closest('.panel-group').find('.panel-title').removeClass('collapsed').prop('aria-expanded',"true").attr('aria-expanded',"true");
$(this).closest('.panel-group').find('.panel-collapse').addClass('in').prop('aria-expanded',"true").attr('aria-expanded',"true").css("height","auto");
}else
{
$(this).closest('.panel-group').find('.panel-title').addClass('collapsed').prop('aria-expanded',"false").attr('aria-expanded',"false");
$(this).closest('.panel-group').find('.panel-collapse').removeClass('in').prop('aria-expanded',"false").attr('aria-expanded',"false");
}
setTimeout(function(){$( window ).resize();},200);
});
</script>
回答6:
$('#accordion .collapse').addClass("in")
回答7:
$("#hideAccordianCards").click(function() { $('.collapse').removeClass('show'); });
$("#showAccordianCards").click(function() { $('.collapse').addClass('show'); });
来源:https://stackoverflow.com/questions/22057505/bootstrap-collapse-expand-all