我们能得到弹窗被解雇的方式一样的情态动词,即。使他们关闭时,用户点击以外的地方?

不幸的是,我不能用真实的模态来代替弹窗,因为模态意味着位置:固定,那样就没有弹窗了。:(


当前回答

答案很好,只要添加一个angular指令,以防你像我一样使用angular:

app.directive('popover', ['$document', function($document) {
    return {
        restrict: 'EA',
        link: function(scope, elem, attrs) {
            $(document).ready(function() {
                $('[data-toggle="popover"]').popover();
            });

            elem.bind('click', function(e) {
                $('#notification').popover('toggle');
            })

            $('body').on('click', function (e) {
                //the 'is' for buttons that trigger popups
                //the 'has' for icons within a button that triggers a popup
                if (!elem.is(e.target)
                    && elem.has(e.target).length === 0
                    && $('.popover').has(e.target).length === 0) {
                    elem.popover('hide');
                }
            });
        }
    };
}]);

html代码:

<a popover tabindex="0" role="button"
   id="notification"
   data-toggle="popover" data-trigger="manual"
   data-container="body" data-placement="bottom"
   data-content="This is a popover">
   Popover button
</a>

它应该像使用data-trigger='click focus'一样简单,因为根据bootstrap:

弹出窗口如何触发-点击|悬停|焦点|手动。你可以传递多个触发器;用一个空格把它们分开。手动不能与任何其他触发器结合使用。

然而,由于未知的原因,同时使用点击和聚焦并不适合我,相反,我必须手动切换它。

其他回答

接受答案的最新更新有一个问题: 如果您启用了一些工具提示,然后打开一个弹出窗口,然后单击具有工具提示的元素,则工具提示的文本将显示为弹出窗口。

为了防止这种情况,只需添加

 if ($(this).data('bs.popover')) {

在if-statement内部(或将其与&&连接到if-statement中)

// only show one popover at the time and hide on clicking outside
$(document).on('click', function (e) {
    $('[data-toggle="popover"],[data-original-title]').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) {
            if ($(this).data('bs.popover')) {
                (($(this).popover('hide').data('bs.popover') || {}).inState || {}).click = false  // fix for BS 3.3.6
            }
        }
    });
});

所谓的高投票的解决方案对我来说都不正确。 当第一次打开和关闭(通过单击其他元素)弹出窗口后,它不会再次打开,直到你在触发链接上单击两次而不是一次,这将导致一个错误。

所以我稍微修改了一下:

$(document).on('click', function (e) {
    var
        $popover,
        $target = $(e.target);

    //do nothing if there was a click on popover content
    if ($target.hasClass('popover') || $target.closest('.popover').length) {
        return;
    }

    $('[data-toggle="popover"]').each(function () {
        $popover = $(this);

        if (!$popover.is(e.target) &&
            $popover.has(e.target).length === 0 &&
            $('.popover').has(e.target).length === 0)
        {
            $popover.popover('hide');
        } else {
            //fixes issue described above
            $popover.popover('toggle');
        }
    });
})

这个问题之前已经被问到过了。我当时给出的答案仍然适用:

我也有类似的需求,并找到了这个很棒的小扩展的Twitter引导弹窗由李卡迈克尔,称为BootstrapX -点击。他还提供了一些用法示例。基本上,它会将弹出窗口更改为一个交互式组件,当您单击页面的其他地方或弹出窗口中的关闭按钮时,该组件将关闭。这也将允许多个弹窗一次打开和一堆其他不错的功能。

对于任何人寻找一个解决方案,工作与Bootstrap 5和没有jQuery,即使弹出窗口是动态生成(即手动触发):

document.querySelector('body').addEventListener('click', function(e) {
    var in_popover = e.target.closest(".popover");

    if (!in_popover) {
        var popovers = document.querySelectorAll('.popover.show');

        if (popovers[0]) {
            var triggler_selector = `[aria-describedby=${popovers[0].id}]`;

            if (!e.target.closest(triggler_selector)) {
                let the_trigger = document.querySelector(triggler_selector);
                if (the_trigger) {
                    bootstrap.Popover.getInstance(the_trigger).hide();
                }
            }
        }
    }
});

这个派对来得太晚了……但我想分享一下 我喜欢弹窗,但是它的内置功能太少了。我写了一个引导扩展.bubble(),这就是我想要的弹窗的一切。四种解题方法。单击外部,切换链接,单击X,然后单击escape。

它会自动定位,所以永远不会离开页面。

https://github.com/Itumac/bootstrap-bubble

这不是毫无理由的自我推销……在我的生活中,我已经多次获得别人的代码,我想贡献我自己的努力。试一试,看看是否对你有用。