2016-08-11 77 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> 

的Javascript

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

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

回答

0

實際上,您的JavaScript事件同時針對.panel-collapse。您應該只將目標面板放在.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'); 
});