Bootstrapを使用して、折りたたみ可能なネストされたdiv /ボタンを作成します。展開されている場合、divのすべての兄弟を折りたたみ、折りたたまれている場合、divのすべての子を折りたたみます(そのため、展開されたdivは、ルートdivの1つから内部divへの1つのパスのみからのものです。
私は、Bootstrapのアコーディオンを使用せずに、jQueryでそれを行うことを選択します-divが関連付けられたボタンのみ。
したがって、展開可能なすべてのdivを選択してshow.bs.collapseイベントを使用して、どのdivが展開されているかを確認しようとしました。
$(function () {
$(".collapse").on('show.bs.collapse', function () {
console.log ($(this).context.id);
})
});
私の木がそうであるならば:
1-拡張
2-拡張
4-崩壊
5-崩壊
6-崩壊
私は3を展開します、私は得ません:
3
だが:
3
2
1
Show.bs.collapseがdiv 1と2に適用されるのはなぜですか?
試す
$(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>
代わりにshown.bs.collapse
を使用してみてください。 http://www.tutorialspoint.com/bootstrap/bootstrap_collapse_plugin.htm
セクションThe following table lists a few events that can be used with the collapse functionality.