简体   繁体   中英

Bootstrap: Cancel data-toggle=“collapse” on nested button

I want my panel-collapse to collapse when the panel-heading is clicked but not when the button in the panel-heading is clicked.

My code:

<div class="panel panel-default">
  <div class="panel-heading clearfix" data-toggle="collapse" href="#collapseBody">
    <h4 class="panel-title pull-left" style="padding-top: 7.5px;">Body</h4>
    <div class="btn-group pull-right">
      <!-- The problematic button -->
      <a href="#" class="btn btn-default">Button</a>
    </div>
  </div>
  <div class="panel-collapse collapse in" id="collapseBody">
    <div class="panel-body" id="body">
    </div>
  </div>
</div>

What i've tried:

I tried to add

<script>
    $('.btn').on('click', function (e) {
        e.stopPropagation();
    });
</script>

But without any effect.

Can someone please help out a beginner? (:

adeneo gave the golden tip in his comment.

Fix:

<script>
    $( document ).ready(function() {
        $('.btn').on('click', function (e) {
            e.stopPropagation();
        });
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM