如何在再次单击或单击其他地方时关闭 bootstrap 弹出窗口

how to dismiss bootstrap popover when either click again or click elsewhere

我可以使用 data-trigger='focus' 在外部单击时关闭弹出窗口,或者在再次单击按钮时使用 data-trigger='click'。但我不能两者兼得。也就是说,如果我使用 focus,单击弹出按钮不会执行任何操作。

无论哪种情况,我都想关闭弹出窗口,有人知道如何轻松做到这一点吗?

我试过data-trigger='focus click',但没用。

编辑:我不认为这是一个重复的问题。当然,这个问题的解决方案对我有用。但该问题的标题:"How to dismiss a Twitter Bootstrap popover by clicking outside" 具有误导性。如果我只是想通过单击外部来关闭弹出窗口,我只需要 data-trigger='focus',如 bootstrap 文档所述:

Use the focus trigger to dismiss popovers on the next click that the user makes.

但是正如我所解释的,如果您再次单击弹出窗口触发器,它将不起作用。

如果没有看到任何代码,我不确定你的意思,但我会使用 jQuery:

    $('body').on('click', function (e) {

    if ($(e.target).data('toggle') !== 'popover'
        && $(e.target).parents('.popover.in').length === 0) { 
        $('[data-toggle="popover"]').popover('hide');
    }
});

希望对你有所帮助,祝你好运!