问题
Trying to get the accordion open and collapse only when it's being clicked. I am able to open/collapse it but it is getting closed also when another accordion is clicked.
It should open or close on its own - independently from other accordions.
This is what I have tried.
JS:
$(document).on("click", ".accordion-toggle", function () {
if ($(this).attr('class').indexOf('open') == -1)
$(this).toggleClass("open").next().slideToggle('fast');
//Hide the other panels
$(".accordion-toggle").not($(this)).removeClass("open");
$(".accordion-content").not($(this).next()).slideUp('fast');
});
Fiddle: Demo
回答1:
I have created a new fiddle with some css...
HTML
<div class="accordion-container">
<a href="#" class="accordion-toggle">Heading 1</a>
<div class="accordion-content">
Content 1
</div>
</div>
<div class="accordion-container">
<a href="#" class="accordion-toggle">Heading 2</a>
<div class="accordion-content">
Content 2
</div>
</div>
<div class="accordion-container">
<a href="#" class="accordion-toggle">Heading 3</a>
<div class="accordion-content">
Content 3
</div>
</div>
JS
$(document).ready(function () {
$('.accordion-toggle').on('click', function(event){
event.preventDefault();
// create accordion variables
var accordion = $(this);
var accordionContent = accordion.next('.accordion-content');
// toggle accordion link open class
accordion.toggleClass("open");
// toggle accordion content
accordionContent.slideToggle(250);
});
});
please check this fiddle
回答2:
Easy and simple bootstrap accordion
on a click
HTML
<button type="button" class="btn openpanel">Open panel 3</button>
<button type="button" class="btn closepanel">Close panel 3</button>
<hr>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel1">Panel 1</a>
</h4>
</div>
<div id="panel1" class="panel-collapse collapse">
<div class="panel-body">
Contents panel 1
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel2">Panel 2</a>
</h4>
</div>
<div id="panel2" class="panel-collapse collapse">
<div class="panel-body">
Contents panel 2
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#panel3">Panel 3</a>
</h4>
</div>
<div id="panel3" class="panel-collapse collapse">
<div class="panel-body">
Contents panel 3
</div>
</div>
</div>
</div>
Simple js code
$(".openpanel").on("click", function() {
$("#panel3").collapse('show');
});
$(".closepanel").on("click", function() {
$("#panel3").collapse('hide');
});
$('#accordion').on('show.bs.collapse', function () {
$('#accordion .in').collapse('hide');
});
I hope it will help you.
来源:https://stackoverflow.com/questions/29096037/how-to-open-and-collapse-the-same-jquery-accordion-with-click-on-it-only