代表面板标题单击到内部链接以获取Bootstrap手风琴

时间:2018-07-31 21:45:32

标签: jquery twitter-bootstrap accordion bootstrap-accordion

我有一个引导面板手风琴:

<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>触发内部.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;
});

...但是,当我单击以下命令时,使用答案this question中的代码将什么也不会发生:

panelHeadings.on('click', function (e) {
    $(this).find('a').triggerHandler('click');
    e.stopPropagation();
    e.preventDefault();
});

还有另一种方法可以触发内部a上的click事件吗?

1 个答案:

答案 0 :(得分:1)

这是一种实现方法-使用{{3}}:

锚定标记阻止了默认事件,并且新事件已添加到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>

希望这会有所帮助。