使用Bootstrap 4,我设置了一个弹出窗口以显示一些HTML内容。在弹出窗口内部,我想使用jQuery显示/隐藏具有其他内容的div,但无法显示隐藏的内容。
的HTML
<div class="text-center">
<button id="show-popover" type="button" class="btn btn-primary mt-5" data-toggle="popover">Click Me</button>
</div>
<!-- popover content -->
<div id="popover-content" class="d-none">
<div class="card border-0">
<div class="card-body">
<a href="#" id="show-div">Show more content</a>
<div id="hidden-content" class="d-none">
div with more content...
</div>
</div>
</div>
</div>
jQuery的
$(function () {
$('#show-popover').popover({
container: 'body',
html: true,
placement: 'bottom',
sanitize: false,
content: function() {
return $('#popover-content').html();
}
})
});
$('#show-div').click(function(){
$('#hidden-content').toggle();
});
当你正在追加即:$('#popover-content').html()
动态内容酥料饼的,所以你需要你的点击处理程序绑定到一些静态元素,并使用class
选择,而不是id
得到的参考hidden-content
使用 $(this).parent().find('.hidden-content').toggle();
隐藏和显示相同。
演示代码:
$(function() {
$('#show-popover').popover({
container: 'body',
html: true,
placement: 'bottom',
sanitize: false,
content: function() {
return $('#popover-content').html();
}
})
});
//call handler like below ..
$(document).on('click', '.card-body > #show-div', function() {
//then use .find to get the div and then show/hide
$(this).parent().find('.hidden-content').toggle();
});
.d-none {
display: none
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="text-center">
<button id="show-popover" type="button" class="btn btn-primary mt-5" data-toggle="popover">Click Me</button>
</div>
<!-- popover content -->
<div id="popover-content" class="d-none">
<div class="card border-0">
<div class="card-body">
<a href="#" id="show-div">Show more content</a>
<div class="hidden-content d-none">
div with more content...
</div>
</div>
</div>
</div>