如何点击外面关闭Twitter Bootstrap popover?

Ant*_*rli 279 popover twitter-bootstrap

我们能否以与模态相同的方式使弹出窗口被解雇,即.当用户点击它们之外的某个地方时,让它们关闭?

不幸的是我不能只使用真正的模态而不是弹出模式,因为模态意味着位置:固定并且不再是弹出模式.:(

mat*_*yer 452

更新:一个稍微强大的解决方案:http://jsfiddle.net/mattdlockyer/C5GBU/72/

对于仅包含文本的按钮:

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

对于包含图标使用的按钮(此代码在Bootstrap 3.3.6中有一个错误,请参阅此答案中的下面的修复)

$('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');
        }
    });
Run Code Online (Sandbox Code Playgroud)

用于JS生成的弹出用途'[data-original-title]'代替'[data-toggle="popover"]'

警告:上面的解决方案允许一次打开多个弹出窗口.

一次一个popover请:

更新: Bootstrap 3.0.x,查看代码或小提琴http://jsfiddle.net/mattdlockyer/C5GBU/2/

$('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)

这样可以处理已经打开但没有点击的弹出窗口的关闭或者没有点击它们的链接.


更新: Bootstrap 3.3.6,请参阅小提琴

修复问题在关闭后,需要2次点击才能重新打开

$(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) {                
            (($(this).popover('hide').data('bs.popover')||{}).inState||{}).click = false  // fix for BS 3.3.6
        }

    });
});
Run Code Online (Sandbox Code Playgroud)

  • 激活弹出窗口(以及随后的隐藏动作)后,弹出窗口不会完全隐藏; 它只是不可见.问题是无法点击或悬停隐形但存在的弹出窗口下的内容.问题出现在最新的Chrome版本,最新的bootstrap 3 .js上(也可能是其他浏览器,因为无论如何都需要这种解决方法,因此无法检查) (6认同)
  • 而不是'[data-toggle ="popover"]'',它不适用于JavaScript生成的弹出窗口,我使用`'[data-original-title]'`作为选择器. (6认同)
  • 有谁知道为什么这个解决方案不适用于最新版本的bootstrap?发生以下情况:单击按钮显示弹出窗口,然后单击正文以关闭弹出窗口,然后单击按钮以显示弹出窗口并且弹出窗口不显示.如果再次单击它会失败一次,它会显示.这很奇怪. (4认同)
  • @JTunney我正在运行BS 3.3.6并且仍然看到在解雇一个之后需要两次点击才能打开一个popo的行为. (3认同)
  • 我附加到`$(document)`而不是`$('body')`因为有时`body`不会延伸到整个页面. (2认同)
  • 所以隐藏但仍然活跃的弹出问题仍然存在.可以使用一点css修复:.popover.fade {z-index:-1} .popover.fade.in {z-index:99}. (2认同)
  • 两次点击bug的解决方法仍然没有修复:`$(this).popover('hide').data('bs.popover').inState.click = false` (2认同)
  • 令人难以置信,我在几年前实施了您的解决方案,但在更新后无效.我不能相信我会不时地看到你的帖子,并提供更新的解决方案.我希望我可以加倍你的答案.期待在V4退出时检查您的解决方案. (2认同)

小智 56

$('html').on('mouseup', function(e) {
    if(!$(e.target).closest('.popover').length) {
        $('.popover').each(function(){
            $(this.previousSibling).popover('hide');
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

如果您点击弹出窗口以外的任何地方,这将关闭所有弹出窗口

Bootstrap 4.1的更新

$("html").on("mouseup", function (e) {
    var l = $(e.target);
    if (l[0].className.indexOf("popover") == -1) {
        $(".popover").each(function () {
            $(this).popover("hide");
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 有3个弹出按钮,这个代码会产生问题.在某些情况下,我无法点击按钮,按钮闪烁. (2认同)

guy*_*uya 38

最简单,最安全的故障版本适用于任何引导程序版本.

演示:http: //jsfiddle.net/guya/24mmM/

演示2:点击popover内容http://jsfiddle.net/guya/fjZja/时不要解雇

演示3:多个弹出窗口:http: //jsfiddle.net/guya/6YCjW/


简单地调用此行将解除所有弹出窗口:

$('[data-original-title]').popover('hide');
Run Code Online (Sandbox Code Playgroud)

使用此代码单击外部时,关闭所有弹出窗口:

$('html').on('click', function(e) {
  if (typeof $(e.target).data('original-title') == 'undefined') {
    $('[data-original-title]').popover('hide');
  }
});
Run Code Online (Sandbox Code Playgroud)

上面的代码段会在主体上附加一个点击事件.当用户单击一个弹出框时,它将表现正常.当用户点击不是popover的东西时,它将关闭所有弹出窗口.

它也适用于使用Javascript启动的弹出窗口,而不是其他一些不起作用的示例.(见演示)

如果您不想在单击popover内容时忽略,请使用此代码(请参阅第2个演示的链接):

$('html').on('click', function(e) {
  if (typeof $(e.target).data('original-title') == 'undefined' && !$(e.target).parents().is('.popover.in')) {
    $('[data-original-title]').popover('hide');
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 有一个类似的问题,这在Bootstrap 3中有效. (3认同)

per*_*lis 19

使用bootstrap 2.3.2,您可以将触发器设置为"焦点",它只是起作用:

$('#el').popover({trigger:'focus'});
Run Code Online (Sandbox Code Playgroud)


She*_*row 18

这基本上不是很复杂,但有一些检查可以避免故障.

演示(jsfiddle)

var $poped = $('someselector');

// Trigger for the popover
$poped.each(function() {
    var $this = $(this);
    $this.on('hover',function() {
            var popover = $this.data('popover');
            var shown = popover && popover.tip().is(':visible');
            if(shown) return;        // Avoids flashing
            $this.popover('show');
    });
});

// Trigger for the hiding
 $('html').on('click.popover.data-api',function() {
    $poped.popover('hide');
});
Run Code Online (Sandbox Code Playgroud)

  • 当然,但是你需要在传递给click处理程序的事件上调用`stopPropagation()`(如果没有,_hiding_ handler会立即隐藏popover).[演示(jsfiddle)](http://jsfiddle.net/Sherbrow/e6Gt8/163/) (3认同)
  • 这通过点击不在外面的任何地方来解散模态 (2认同)
  • 更正,我相信我在FAR代码中有更好的功能.它假设您一次只能看到一个弹出窗口.如果你喜欢这个,请在​​下面提出我的答案:http://jsfiddle.net/P3qRK/1/ answer:http://stackoverflow.com/a/14857326/1060487 (2认同)

Ant*_*yev 16

所谓的高投票解决方案都没有正确地为我工作.每次打开和关闭(通过单击其他元素)弹出窗口时,每个都会导致错误,它不再打开,直到您在触发链接而不是一次点击两次.

所以我稍微修改了一下:

$(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');
        }
    });
})
Run Code Online (Sandbox Code Playgroud)


Pig*_*ras 11

我做了一个jsfiddle来告诉你如何做到这一点:

http://jsfiddle.net/3yHTH/

想法是在单击按钮时显示弹出窗口,并在单击按钮外部时隐藏弹出窗口.

HTML

<a id="button" href="#" class="btn btn-danger">Click for popover</a>
Run Code Online (Sandbox Code Playgroud)

JS

$('#button').popover({
    trigger: 'manual',
    position: 'bottom',
    title: 'Example',
    content: 'Popover example for SO'
}).click(function(evt) {
    evt.stopPropagation();
    $(this).popover('show');
});

$('html').click(function() {
    $('#button').popover('hide');
});
Run Code Online (Sandbox Code Playgroud)

  • 我唯一的问题是你单击它时隐藏弹出窗口.不妨使用工具提示. (2认同)

小智 9

只需在元素中添加此属性

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


Ely*_*yor 6

Bootstrap 5 更新:

$(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-bs-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');
        } 
    });
})
Run Code Online (Sandbox Code Playgroud)


eff*_*ffe 5

根据http://getbootstrap.com/javascript/#popovers

<button type="button" class="popover-dismiss" data-toggle="popover" title="Dismissible popover" data-content="Popover Content">Dismissible popover</button>
Run Code Online (Sandbox Code Playgroud)

使用焦点触发器在用户下一次单击时关闭弹出窗口。

$('.popover-dismiss').popover({
    trigger: 'focus'
})
Run Code Online (Sandbox Code Playgroud)

  • 好吧,这个答案*有*一个问题,它与平台兼容性无关:按下鼠标按钮*在弹出窗口内*将关闭弹出窗口,因为触发弹出窗口的元素将失去焦点。忘记让用户能够从弹出窗口复制和粘贴:只要按下鼠标按钮,弹出窗口就会关闭。如果您在弹出窗口中有可操作的项目(按钮、链接),用户将无法使用它们。 (3认同)
  • 不适用于遵循 OS X 本机行为的 Mac 浏览器(点击时不聚焦也不模糊按钮)。其中包括 Firefox 和 Safari。Bootstrap 的人在这里犯了一个大错误,因为这些弹出窗口甚至无法打开,更不用说解雇了。 (2认同)
  • @AnteVrli 也许在您撰写评论时文档中尚未包含此内容,但现在文档说:“对于正确的跨浏览器和跨平台行为,您必须使用 `&lt;a&gt;` 标签,而不是 `&lt;button&gt; ` 标记,并且您还必须包含 `role="button"` 和 `tabindex` 属性。" 你试过这些规格吗? (2认同)

rav*_*avi 5

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

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

来自https://getbootstrap.com/docs/3.3/javascript/#dismiss-on-next-click 的参考