2016-08-11 4 views
-1

나도 같은 페이지에 여러 부트 스트랩 아코디언을 가지고 이벤트가 모두 영향을 줄 수있는 하나의 아코디언에 대한 트리거 것으로 나타났습니다 (예 : 열기/닫기 하나의 아코디언 효과의 모든 아코디언을.)부트 스트랩 아코디언 범위 문제

변경하지 않고 마크 업 및 $ (이)를 사용하지 않고, 범위에 대한 방법은 다음 예? https://jsfiddle.net/terra823/kgrjg2z3/1/

HTML이

<div class="container"> 
    <a href="#" class="btn btn-default openall">open all</a> <a href="#" class="btn btn-default closeall">close all</a> 
    <hr> 
    <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="#collapseOne"> 
      Collapsible Group Item #1 
      </a> 
     </h4> 
     </div> 
     <div id="collapseOne" class="panel-collapse collapse in"> 
     <div class="panel-body"> 
      ONe 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="#collapseTwo"> 
      Collapsible Group Item #2 
      </a> 
     </h4> 
     </div> 
     <div id="collapseTwo" class="panel-collapse collapse"> 
     <div class="panel-body"> 
      Two 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> 
     </h4> 
     </div> 
     <div id="collapseThree" class="panel-collapse collapse"> 
     <div class="panel-body"> 
      Three 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> 
</div> 

<div class="container"> 
    <a href="#" class="btn btn-default openall">open all</a> <a href="#" class="btn btn-default closeall">close all</a> 
    <hr> 
    <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="#collapseOne"> 
      Collapsible Group Item #1 
      </a> 
     </h4> 
     </div> 
     <div id="collapseOne" class="panel-collapse collapse in"> 
     <div class="panel-body"> 
      ONe 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="#collapseTwo"> 
      Collapsible Group Item #2 
      </a> 
     </h4> 
     </div> 
     <div id="collapseTwo" class="panel-collapse collapse"> 
     <div class="panel-body"> 
      Two 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> 
     </h4> 
     </div> 
     <div id="collapseThree" class="panel-collapse collapse"> 
     <div class="panel-body"> 
      Three 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> 
</div> 

자바 스크립트

$('.closeall').click(function(){ 
    $('.panel-collapse.in') 
    .collapse('hide'); 
}); 

$('.openall').click(function(){ 
    $('.panel-collapse:not(".in")') 
    .collapse('show'); 
}); 

답변

0

사실 자바 스크립트 이벤트는 모두 패널 붕괴를 대상으로합니다. 다음과 같은 .container 범위의 패널 만 타겟팅해야합니다.

// For close all button 
$('.closeall').click(function(){ 
    $(this).closest('.container').find('.panel-collapse.in').collapse('hide'); 
}); 

// For open all button 
$('.openall').click(function(){ 
    $(this).closest('.container').find('.panel-collapse:not(".in")').collapse('show'); 
}); 

즐기십시오.

0

확인, 예를 들어, 아코디언의 array index를 사용

*/$('.closeall').click(function(){ 
    $('.panel-collapse.in') 
    .eq(0) // item at the 0. position 
    .collapse('hide'); 
    }); 

이는 첫 번째 항목을 닫습니다.

0

을 사용하여 $(this)을 사용하십시오. 이 문제는 쉽게 해결할 수 있습니다.

다음 코드

$('.closeall').click(function(){ 
    $(this).parent().find('.panel-collapse.in').collapse('hide'); 
}); 

$('.openall').click(function(){ 
    $(this).parent().find('.panel-collapse').collapse('show'); 
}); 
을 사용하십시오
관련 문제