Bootstrap popover,隐藏在外面点击?

Tom*_*mmy 21 jquery twitter-bootstrap

使用bootstrap popover,现在我试图让这个代码在popover外面点击以关闭popover:

$('body').on('click', function (e) {
    $('[data-toggle="popover"]').each(function () {
        //the 'is' for buttons that trigger popups
        //the 'has' for icons within a button that triggers a popup
        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
            $(this).popover('hide');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

但是,当我使用这个部分时,我可以关闭弹出窗口,但我不能点击其他按钮,任何人都知道我怎么能这样做?

所有按钮:

<a href="#" class="btn btn-xs btn-primary" data-toggle="popover">This opens popover</a>
<a href="#" class="btn btn-xs btn-primary">Other link</a> <- Doesn't work 
<a href="#" class="btn btn-xs btn-primary">Other link</a> <- Doesn't work 
Run Code Online (Sandbox Code Playgroud)

Pim*_*Web 31

我发现了这个:http://bootply.com/99372

$('body').on('click', function (e) {
    $('[data-toggle=popover]').each(function () {
        // hide any open popovers when the anywhere else in the body is clicked
        if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.popover').has(e.target).length === 0) {
            $(this).popover('hide');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

这几乎与你的代码相同......

  • 我建议使用“mousedown”事件而不是“click”。这样,在长时间选择或拖放期间也会隐藏弹出窗口。 (2认同)

rav*_*avi 10

只需添加此元素即可在下次单击时关闭弹出窗口。

data-trigger="focus" 
Run Code Online (Sandbox Code Playgroud)

来自这里的参考: Bootstrap Popover

  • 我不是拒绝投票的人,但原因可能是这不是那个人问的。他(和我)希望弹出框只有在单击它的外部时才会消失。data-trigger="focus" 在点击内部时也会隐藏它。 (2认同)

小智 6

事实上,你甚至不需要 JS,因为在 Bootstrap 中已经有一个设置。

请参阅:http : //getbootstrap.com/javascript/#dismiss-on-next-click

“下一次点击关闭所需的特定标记为了正确的跨浏览器和跨平台行为,您必须使用<a>标签,而不是<button>标签,并且您还必须包括 role="button" 和 tabindex 属性。”

前任。:

<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
Run Code Online (Sandbox Code Playgroud)

  • 虽然这不是最完整的答案,但这是正确的方法。但是,如果您选择将任何交互式内容放入其中,这将不起作用,因为所有点击都会关闭弹出窗口。 (8认同)
  • 这将在第二次单击时关闭弹出窗口,而不是在外部单击时关闭。即使我在弹出窗口内单击,这也会关闭 (3认同)

小智 5

尝试这个。单击弹出窗口的外部时,它将关闭弹出窗口。

$('body').on('click', function (e) {
//did not click a popover toggle, or icon in popover toggle, or popover
if ($(e.target).data('toggle') !== 'popover' && $(e.target).parents('[data-toggle="popover"]').length === 0
    && $(e.target).parents('.popover.in').length === 0) {
    (($('[data-toggle="popover"]').popover('hide').data('bs.popover') || {}).inState || {}).click = false;
}
});
Run Code Online (Sandbox Code Playgroud)

另一种解决方案,

<a tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?">Dismissible popover</a>
Run Code Online (Sandbox Code Playgroud)

添加tabindex="0"data-trigger="focus"