我已经搜索并找到了一些类似的答案-但这是围绕在我未使用的手风琴上。
我嵌套了折叠项as seen in this codepen
$('#collapseOutter').on('show.bs.collapse', function(event) {
$('#collapseBtn').html("Show Less");
console.log('Only happen when outter div is opened');
event.stopPropagation();
console.log('even with stopping propagation it fires');
})
$('#collapseOutter').on('hide.bs.collapse', function() {
$('#collapseBtn').html("Show More");
console.log('Only happen when outter div is collapsed');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row ml-4">
<div class="row header-div">
<a id="collapseBtn" class="btn-info btn" data-toggle="collapse" href="#collapseOutter" role="button" aria-expanded="false" aria-controls="collapseOutter">
Show More
</a>
</div>
</div>
<div class="row ml-4">
<div id="collapseOutter" class="collapse">
<p>Here is a paragraph about the data you're about to see.</p>
<div class="row">
<div id="item-a">
<a id="item-a" class="" data-toggle="collapse" href="#collapseInnerA">
Item One of a list
</a>
<br />
<div id="collapseInnerA" class="collapse">
<div class="row">
<p class="ml-4">Item One Related Information</p>
</div>
</div>
</div>
</div>
<div class="row">
<div id="item-b">
<a id="item-b" class="" data-toggle="collapse" href="#collapseInnerB">
Item Two of a list
</a>
<br />
<div id="collapseInnerB" class="collapse">
<div class="row">
<p class="ml-4">Item Two Related Information</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
当我单击列表中的项目(位于第一个折叠内)时,它会触发父方法。
当我单击列表项以折叠/显示其信息时,如何防止父方法触发?
我正在寻找如何进行与引导程序相关的操作。
答案 0 :(得分:1)
当事件到达外部折叠时,您正在停止事件传播。您应该在内部坍塌处停止它。在this codepen中,第一个内部塌陷的塌陷事件将在第二个停止时冒泡。
$('#collapseInnerB').on('show.bs.collapse', function( event ) {
event.stopPropagation();
})
$('#collapseInnerB').on('hide.bs.collapse', function( event ) {
event.stopPropagation();
})