请考虑以下jQuery
代码片段
function toggleIcon(e) {
$(e.target)
.prev('.panel-heading')
.find(".more-less")
.toggleClass('glyphicon-plus glyphicon-minus');
}
$('.panel-group').on('hidden.bs.collapse', toggleIcon);
$('.panel-group').on('shown.bs.collapse', toggleIcon);
以上代码位于$(document).ready(function(){});
标记看起来像:
<div class="panel-group" id="collapse">
<div class="panel panel-primary">
<div class="panel-heading">
<h4 class="panel-title"><a data-parent="#collapse" data-toggle="collapse" href="#collapse-one"><i class="more-less glyphicon glyphicon-plus"></i> Item Details
<span class="badge pull-right" id="itemdetails">0</span></a></h4>
</div>
<div class="panel-collapse collapse in" id="collapse-one">
<div class="panel-body">
@for (int i = 0; i < Model.MyList.Count; i++) {
<div class="panel panel-info">
<div class="panel-heading">
<h4 class="panel-title"><a data-toggle="collapse"
href="#@Model.MyList[i].Number"><i class="more-less glyphicon glyphicon-plus">@Model.MyList[i].Number, @Model.MyList[i].PropA, @Model.MyList[i].PropB</i></a></h4>
</div>
<div class="panel-collapse collapse" id="@Model.MyList[i].Number">
<div class="panel-body"></div>
</div>
</div>
</div>
</div>
}
</div>
</div>
这适用于除第一个之外的每个面板。第一个面板将始终打开,而不是添加加号,它会添加减号。第一个面板具有类panel-collapse collapse in
,可确保在页面加载时打开面板。
请注意,我有嵌套面板,这也适用于那些,它只是第一个导致我出现问题的面板
有人可以告诉我,我做错了吗。
答案 0 :(得分:0)
我试过这个请参考下面的代码:
<强> HTML 强>:
<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="#collapseTwo">
Collapsible Group Item #2
</a><i class="indicator glyphicon glyphicon glyphicon-plus pull-right"></i>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</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="#collapseThree">
Collapsible Group Item #3
</a><i class="indicator glyphicon glyphicon glyphicon-plus pull-right"></i>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
</div>
</div>
</div>
</div>
JavaScript代码:
$('#accordion').on('hidden.bs.collapse', function () {
//do something...
})
$('#accordion .accordion-toggle').click(function (e) {
var chevState = $(e.target).siblings("i.indicator").toggleClass('glyphicon-minus glyphicon-plus');
$("i.indicator").not(chevState).removeClass("glyphicon-minus").addClass("glyphicon-plus");
});
请参阅此fiddle link以获取演示。