jQuery show.bs.collapse event for nested Bootstrap collapsible buttons fires on more buttons than expected

前端 未结 3 1466
庸人自扰
庸人自扰 2021-02-14 12:55

I\'m using Bootstrap to make collapsible nested divs/buttons. I want to collapse all siblings of a div when it is expanded and to collapse all children of a div when it is colla

相关标签:
3条回答
  • 2021-02-14 13:32

    Try

    $(function() {
      $(".collapse").on('show.bs.collapse', function(e) {
        if ($(this).is(e.target)) {
          snippet.log(this.id)
        }
      })
    });
    <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
    <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
    
    <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap-theme.css">
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/js/bootstrap.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="true" aria-controls="collapseOne">
              Collapsible Group Item #1
            </a>
          </h4>
        </div>
        <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            <div class="panel-group" id="accordion1-1" role="tablist" aria-multiselectable="true">
              <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headingOne-1">
                  <h4 class="panel-title">
                    <a role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseOne-1" aria-expanded="true" aria-controls="collapseOne">
                      Collapsible Group Item #1-1
                    </a>
                  </h4>
                </div>
                <div id="collapseOne-1" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne-1">
                  <div class="panel-body">
                    Collapsible Group Item #1-1
                  </div>
                </div>
              </div>
              <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headingTwo-1">
                  <h4 class="panel-title">
                    <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseTwo-1" aria-expanded="false" aria-controls="collapseTwo">
                      Collapsible Group Item #2-1
                    </a>
                  </h4>
                </div>
                <div id="collapseTwo-1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo-1">
                  <div class="panel-body">
                    <div class="panel-group" id="accordion1-1-2" role="tablist" aria-multiselectable="true">
                      <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingOne-1-2">
                          <h4 class="panel-title">
                            <a role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseOne-1-2" aria-expanded="true" aria-controls="collapseOne">
                              Collapsible Group Item #1-1-2
                            </a>
                          </h4>
                        </div>
                        <div id="collapseOne-1-2" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne-1-2">
                          <div class="panel-body">
                            Collapsible Group Item #1-1-2
                          </div>
                        </div>
                      </div>
                      <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingTwo-1-2">
                          <h4 class="panel-title">
                            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseTwo-1-2" aria-expanded="false" aria-controls="collapseTwo">
                              Collapsible Group Item #2-1-2
                            </a>
                          </h4>
                        </div>
                        <div id="collapseTwo-1-2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo-1-2">
                          <div class="panel-body">
                            Collapsible Group Item #2-1-2
                          </div>
                        </div>
                      </div>
                      <div class="panel panel-default">
                        <div class="panel-heading" role="tab" id="headingThree-1-2">
                          <h4 class="panel-title">
                            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1-2" href="#collapseThree-1-2" aria-expanded="false" aria-controls="collapseThree-1-2">
                              Collapsible Group Item #3-1-2
                            </a>
                          </h4>
                        </div>
                        <div id="collapseThree-1-2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree-1-2">
                          <div class="panel-body">
                            Collapsible Group Item #3-1-2
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
              <div class="panel panel-default">
                <div class="panel-heading" role="tab" id="headingThree-1">
                  <h4 class="panel-title">
                    <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion1-1" href="#collapseThree-1" aria-expanded="false" aria-controls="collapseThree-1">
                      Collapsible Group Item #3-1
                    </a>
                  </h4>
                </div>
                <div id="collapseThree-1" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree-1">
                  <div class="panel-body">
                    Collapsible Group Item #3-1
                  </div>
                </div>
              </div>
            </div>
          </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">
              Collapsible Group Item #2
            </a>
          </h4>
        </div>
        <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            Collapsible Group Item #2
          </div>
        </div>
      </div>
      <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingThree">
          <h4 class="panel-title">
            <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
              Collapsible Group Item #3
            </a>
          </h4>
        </div>
        <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            Collapsible Group Item #3
          </div>
        </div>
      </div>
    </div>

    0 讨论(0)
  • 2021-02-14 13:32

    you can try below code -

    $('.accordion-toggle').click(function(){
           $(this).parent()
                   .find("i.indicator")
                   .toggleClass('fa-caret-down fa-caret-up');
      });
    
    0 讨论(0)
  • 2021-02-14 13:43

    Try using shown.bs.collapse instead. http://www.tutorialspoint.com/bootstrap/bootstrap_collapse_plugin.htm
    section The following table lists a few events that can be used with the collapse functionality.

    0 讨论(0)
提交回复
热议问题