我想通过单击外部(或在屏幕上单击)关闭弹出窗口。在这种情况下,我做错了什么?
HTML
<div class="container">
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover Example</a>
<div>
<button type="button" class="btn btn-warning">Hide</button>
</div>
</div>JS
$(document).ready(function(){
$("[data-toggle='popover']").click(function(){
$("[data-toggle='popover']").popover('show');
});
$(body).click(function(){
$("[data-toggle='popover']").popover('hide');
});
});发布于 2016-04-13 15:39:04
你的代码中有两个问题:
1) $(body)应为$('body')。标记名选择器需要用引号括起来。
2)停止父体事件传播,弹出点击,使show函数工作。
$("[data-toggle='popover']").click(function(e){
e.stopPropagation()
$("[data-toggle='popover']").popover('show');
});
$('body').click(function(){
$("[data-toggle='popover']").popover('hide');
});发布于 2016-04-13 16:01:51
尝试下面的代码可能会帮助你解决这个问题
$(document).ready(function(){
$("[data-toggle='popover']").click(function(){
$("[data-toggle='popover']").popover('show');
return false;
});
$('body').click(function(){
$("[data-toggle='popover']").popover('hide');
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div class="container">
<a href="#" data-toggle="popover" title="Popover Header" data-content="Some content inside the popover">Popover Example</a>
<div>
<button type="button" class="btn btn-warning">Hide</button>
</div>
</div>
https://stackoverflow.com/questions/36591588
复制相似问题