简体   繁体   English

仅在身体单击上隐藏弹出窗口

[英]Hide popover on body click only if already visible

Hiding a div (for example #popover ) on click is easy: 在点击时隐藏div(例如#popover )很容易:

$(document).ready(function() {
    $("#trigger").click(function(e) {
        $("#popover").toggle();
        e.stopPropagation();
    });

    $(document).click(function(e) {
        if (!$(e.target).is('#popover, #popover *')) {
            $("#popover").hide();
        }
    });
});

But isn't this function fired every time the user uses a click ? 但是,不是用户每次click都触发此功能吗? (Which does not make sense for me). (这对我来说没有意义)。 Can I somehow limit this function to fire only if #popover is already visible? 我能否以某种方式将此功能限制为仅在#popover已经可见的情况下才能触发?

In that case you can try using $(document).one(function(e) {...}); 在这种情况下,您可以尝试使用$(document).one(function(e) {...}); every time you show #example . 每次显示#example

$(document).ready(function() {
    var didBindToBody = false;

    function closePopOverOnBodyClick() {
      didBindToBody = true;

      $(document).one('click', function(e) {             
          if (!$(e.target).is('#popover, #popover *')) {
              $("#popover").hide();
          }
          else {
              closePopOverOnBodyClick();
          }

          didBindToBody = false;
      });
    }

    $("#trigger").click(function(e) {
        $("#popover").toggle();
        e.stopPropagation();

        if (!didBindToBody && $("#popover").is(":visible")) {
            closePopOverOnBodyClick();
        }  
    });

    closePopOverOnBodyClick();
});

DEMO 演示

You could try something like 您可以尝试类似

$(document).click(function(e) {

    if (!$('#example').is(":visible")) {
        return false;
    } else if (!$(e.target).is('#popover, .login-panel *')) {
        $("#popover").hide();
    }
});

Or you could combine the two conditionals into one, just thought I'd keep it separate for readability and to make my suggestion easier to spot. 或者,您可以将这两个条件组合为一个,只是为了提高可读性并使我的建议更容易理解而将其分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引导弹出窗口隐藏在外部单击上。 仅在第二次点击时打开 - Bootstrap popover hide on outside click. Opens only on second click JS切换可见性:如何在点击时隐藏已经可见的元素 - JS Toggle Visibility: How to Hide Already Visible Elements on Click Twitter bootstrap popover:如何在不使用主体/文档事件处理程序的情况下在外部单击时将其隐藏? - Twitter bootstrap popover : How to hide it on click outside, without using the body/document event handler? AngularJS popover-隐藏在主体上(在小提琴中单击) - AngularJS popover - hide on clicking on body (fiddle inside) 身体溢出隐藏在点击 - Body overflow hide on click 隐藏身体上的子菜单 - hide submenu on body click 引导程序 5 弹出窗口中的关闭按钮并在正文单击时关闭弹出窗口 - Close button in bootstrap 5 popover and close popover on body click 如何在 Angular JS 的弹出窗口外单击时隐藏 uib-popover? - How to hide uib-popover on click outside of the popover in angular JS? 崩溃时可见,如何通过单击身体隐藏我的崩溃bootstrap 3导航栏? - How to hide my collapse bootstrap 3 navbar with click on body when, collapse is visible? Bootstrap .popover('show'), .popover('hide') 不起作用。 将其绑定到点击作品 - Bootstrap .popover('show'), .popover('hide') not working. Binding it to click works
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM