これを行う簡単な方法は次のとおりです。
ブートストラップ 4
$('.accordion .btn-link').on('click', function(e) {
if (!$(this).hasClass('collapsed')) {
e.stopPropagation();
}
});
コメントの @mr_joncollette から
ブートストラップ 3
Bootstrap 3 のJsFiddle 。
ブートストラップ 3 のコード:
$('.panel-heading a').on('click',function(e){
if($(this).parents('.panel').children('.panel-collapse').hasClass('in')){
e.stopPropagation();
}
// You can also add preventDefault to remove the anchor behavior that makes
// the page jump
// e.preventDefault();
});
このコードは、クリックされた要素が(クラス "in" によって) 現在表示されている要素であるかどうかを確認し、"in" クラスがある場合は、非表示プロセスを停止します。
非推奨のブートストラップ 2
Bootstrap 2 のJsFiddle 。
ブートストラップ 2 のコード:
$('.accordion-toggle').on('click',function(e){
if($(this).parents('.accordion-group').children('.accordion-body').hasClass('in')){
e.stopPropagation();
}
// You can also add preventDefault to remove the anchor behavior that makes
// the page jump
// e.preventDefault();
});
注: チェック後に発生するイベントがブロックされるため、アコーディオンにさらにクリック イベントを追加する場合は注意してください。e.stopPropagation()