代表小组标题点击内部 link 以获得 bootstrap 手风琴
delegate panel heading click to inner link for bootstrap accordion
我有一个 bootstap 面板手风琴:
<div class="modal-body">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
Annual </a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Semi-Annual </a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
</div>
</div>
而不是单击微小的 <a>
link,我希望整个 .panel-heading
触发内部 <a>
.
的单击
由于明显的 Maximum call stack size exceeded
错误,我无法使用以下代码:
panelHeadings.on('click', function (e) {
$(this).find('a').click(); // trigger('click') behaves the same
e.stopPropagation();
e.preventDefault();
return false;
});
...但是使用 答案中的代码导致我单击时没有任何反应:
panelHeadings.on('click', function (e) {
$(this).find('a').triggerHandler('click');
e.stopPropagation();
e.preventDefault();
});
还有其他方法可以触发内a的点击事件吗?
这是一种方法 - 使用 BS-collapse-toggle:
为锚标记阻止了默认事件,并向 panel-heading
添加了一个新事件:
$('div#accordion .panel-heading a[data-toggle="collapse"]').on('click', function (e) {
e.preventDefault();
});
$('div#accordion .panel-heading').on('click', function() {
$(this).parent().find('.collapse').collapse('toggle');
});
$('div#accordion .panel-heading a[data-toggle="collapse"]').on('click', function (e) {
e.preventDefault();
});
$('div#accordion .panel-heading').on('click', function() {
$(this).parent().find('.collapse').collapse('toggle');
});
div#accordion .panel-heading {
cursor: pointer;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
Annual </a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Semi-Annual </a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
</div>
希望对您有所帮助。
我有一个 bootstap 面板手风琴:
<div class="modal-body">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
Annual </a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Semi-Annual </a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
</div>
</div>
而不是单击微小的 <a>
link,我希望整个 .panel-heading
触发内部 <a>
.
由于明显的 Maximum call stack size exceeded
错误,我无法使用以下代码:
panelHeadings.on('click', function (e) {
$(this).find('a').click(); // trigger('click') behaves the same
e.stopPropagation();
e.preventDefault();
return false;
});
...但是使用
panelHeadings.on('click', function (e) {
$(this).find('a').triggerHandler('click');
e.stopPropagation();
e.preventDefault();
});
还有其他方法可以触发内a的点击事件吗?
这是一种方法 - 使用 BS-collapse-toggle:
为锚标记阻止了默认事件,并向 panel-heading
添加了一个新事件:
$('div#accordion .panel-heading a[data-toggle="collapse"]').on('click', function (e) {
e.preventDefault();
});
$('div#accordion .panel-heading').on('click', function() {
$(this).parent().find('.collapse').collapse('toggle');
});
$('div#accordion .panel-heading a[data-toggle="collapse"]').on('click', function (e) {
e.preventDefault();
});
$('div#accordion .panel-heading').on('click', function() {
$(this).parent().find('.collapse').collapse('toggle');
});
div#accordion .panel-heading {
cursor: pointer;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne" class="collapsed">
Annual </a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Semi-Annual </a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo" aria-expanded="false" style="height: 0px;">
<div class="panel-body"></div>
</div>
</div>
</div>
希望对您有所帮助。