这是我的模板:
<div class="panel-heading">
<div class="panel-title">Tableau de bord
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne"><span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span></a>
</div>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<ul class="list-group">
<li class="list-group-item"><i class="fa fa-user"></i><a href="#">Information</a></li>
<li class="list-group-item"><i class="fa fa-power-off"></i><a href="#">Log out</a></li>
</ul><!--/.list-group-->
</div><!--/collapseOne-->
<div class="panel-footer">
<div class="panel-title">Archives
<a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo"><span class="pull-right clickable"><i class="glyphicon glyphicon-chevron-up"></i></span></a>
</div>
</div>
<div id="collapseTwo" class="panel-collapse collapse in">
<ul class="list-group" style="margin-bottom:0">
<li class="list-group-item"><i class="fa fa-arrow-right"></i><a href="#">2015</a></li>
<li class="list-group-item"><i class="fa fa-arrow-right"></i><a href="#">2014</a></li>
</ul><!--/.list-group-->
</div><!--/collapseTwo-->
</div><!--/.panel-blue-->
</div><!--/.panel-group accordion-->
这是脚本:
<script>
$('.clickable').on('click', function (e) {
var $this = $(this);
if (!$this.hasClass('panel-collapsed')) {
$this.parents('.panel').find('.panel-title').slideUp();
$this.addClass('panel-collapse');
$this.find('i').removeClass('glyphicon-chevron-up').addClass('glyphicon-chevron-down');
} else {
$this.parents('.panel').find('.panel').slideDown();
$this.removeClass('.panel-title');
$this.find('i').removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-up');
}
})
</script>
答案 0 :(得分:0)
我用toggleClass替换了两行addClass / removeClass,它们做同样的事情,它似乎有效:http://jsfiddle.net/FLZpk/1/
$('.clickable').on('click', function (e) {
var $this = $(this);
if (!$this.hasClass('panel-collapsed')) {
$this.parents('.panel').find('.panel-title').slideUp();
$this.addClass('panel-collapse');
} else {
$this.parents('.panel').find('.panel').slideDown();
$this.removeClass('.panel-title');
}
$this.find('i').toggleClass('glyphicon-chevron-up glyphicon-chevron-down');
})
jQuery toggleClass doc:https://api.jquery.com/toggleClass/