简体   繁体   English

在外部单击时关闭弹出 div

[英]Close popup div when clicked outside

I have a popup div that shows only when clicked on particular button.我有一个弹出式 div,仅在单击特定按钮时显示。 It even hides when clicked on the same button.单击同一按钮时它甚至会隐藏。 My problem is, i also want to hide div when clicked anywhere outside.我的问题是,我也想在点击外面的任何地方时隐藏 div。 I am not able to do so because the popup div is inside the main wrapper class and can't do so by using click event on the wrapper class and making it hide.我无法这样做,因为弹出 div 位于主包装类内部,并且无法通过在包装类上使用 click 事件并将其隐藏来做到这一点。 This is my code:这是我的代码:

function showHide() {
    var ele = document.getElementById("div_fieldWorkers");

    if (ele.style.display == "block") {           
        ele.style.display = "none";
    } else {
        ele.style.display = "block";      
    }
}

<input type="button" value="Add Field Worker" id="btnFieldWorkers" onclick="return showHide();" class="btn btn-primary" />

The solution to this problem is here: Use jQuery to hide a DIV when the user clicks outside of it这个问题的解决方案在这里: 当用户在 DIV 外部单击时,使用 jQuery 隐藏它

Also, you tagged this question jquery , but your code is pure javascript.此外,您标记了这个问题jquery ,但您的代码是纯 javascript。 When using jQuery, you can write only使用jQuery时,你只能写

$('#div_fieldWorkers').toggle();

in your onclick .在您的onclick 中

Check this out: http://jsfiddle.net/d4SsZ/1/看看这个: http : //jsfiddle.net/d4SsZ/1/

Revised: http://jsfiddle.net/d4SsZ/3/修订: http : //jsfiddle.net/d4SsZ/3/

Just a snippet: Validate for null and undefined js errors if any.只是一个片段:验证 null 和未定义的 js 错误(如果有)。

Markup:标记:

<div id="div_fieldWorkers" class="form_size" style='display:none;' class='noclick'><span class='noclick'>Hello How are you?</span></div>
<input
    type="button"
    value="Add Field Worker"
    id="btnFieldWorkers"
    class="btn btn-primary" />

Javascript: Javascript:

 $('#btnFieldWorkers').bind("click", ToggleDisplay);

function ToggleDisplay() {
    if ($("#div_fieldWorkers").data('shown'))
        hide();
    else 
        display();
}

function display() {    
    if ($("#div_fieldWorkers").children().length > 0) {
        $("#div_fieldWorkers").fadeIn(500, function() {
            $(document).bind("click", function() {hide(); });            
            $("#div_fieldWorkers").data('shown', true)});         
    }  
}

function hide() {   
    if (window.event.toElement.className != 'noclick') {
        $("#div_fieldWorkers").fadeOut(500, function() {
            $(document).unbind("click");
            $("#div_fieldWorkers").data('shown', false);                
        });
    }
}

Had the same problem, came up with this easy solution.有同样的问题,想出了这个简单的解决方案。 It's even working recursive:它甚至可以递归工作:

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

var container = $("YOUR CONTAINER SELECTOR");

if (!container.is(e.target) // if the target of the click isn't the container...
    && container.has(e.target).length === 0) // ... nor a descendant of the container
{
    container.hide();
}
});

Create click event on body element and stopPropagation.在 body 元素和 stopPropagation 上创建单击事件。 this makes the event to call the click event only on the button.这使得事件只在按钮上调用点击事件。 Then check that the click target element isn't the popup div.然后检查单击目标元素是否不是弹出 div。 example:例子:

$(function(){
   $("#btnFieldWorkers").click(function(e){       
       e.stopPropagation();
       $("#div_fieldWorkers").show();
       $("body").click(function(e){
           if(e.target.id != "div_fieldWorkers")
           {
               $("#div_fieldWorkers").hide();
               $("body").unbind("click");
           }
       });
   });

});

jsfiddle提琴手

document.addEventListener('click', function () {
  document.querySelector('.menu').classList.remove('active');
});

document.querySelector('.toggle-menu-btn').addEventListener('click', function (event) {
  document.querySelector('.menu').classList.toggle('active');
  event.stopPropagation();
});

document.querySelector('.menu').addEventListener('click', function (event) {
  event.stopPropagation();
});

您还可以通过单击弹出 div 来隐藏弹出窗口。为此,您必须在 div 标签中提供单击功能。在该单击功能中,您必须编写关闭弹出功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM