admin管理员组

文章数量:1291174

I have this code and I want to close this <div> popup by clicking outside of it and not only by clicking on the close button.

How can I do that?

<script language="JavaScript">
 <!--
function displayPopup(alert_MSG) {

    var theDetail = document.getElementById(alert_MSG);
        theDetail.style.display="block";
}

function closePopup(alert_MSG) {

    var theDetail = document.getElementById(alert_MSG);

    if (theDetail.style.display=="block") {
        theDetail.style.display="none";
    }
}
-->
</script>

Here is the HTML link:

<a href="javascript:displayPopup(<?=$id_pic?>)">open div here</a>

And is here my popup:

<div id="<?=$id_pic?>" class="myPopup" style="display:none;">
<div class="closeButton">
<a href="javascript:closePopup(<?=$id_pic?>)">Close</a>
</div>
Popup content here
</div>

I have this code and I want to close this <div> popup by clicking outside of it and not only by clicking on the close button.

How can I do that?

<script language="JavaScript">
 <!--
function displayPopup(alert_MSG) {

    var theDetail = document.getElementById(alert_MSG);
        theDetail.style.display="block";
}

function closePopup(alert_MSG) {

    var theDetail = document.getElementById(alert_MSG);

    if (theDetail.style.display=="block") {
        theDetail.style.display="none";
    }
}
-->
</script>

Here is the HTML link:

<a href="javascript:displayPopup(<?=$id_pic?>)">open div here</a>

And is here my popup:

<div id="<?=$id_pic?>" class="myPopup" style="display:none;">
<div class="closeButton">
<a href="javascript:closePopup(<?=$id_pic?>)">Close</a>
</div>
Popup content here
</div>
Share Improve this question edited Dec 9, 2012 at 13:59 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Dec 9, 2012 at 13:38 fabikusfabikus 211 gold badge1 silver badge2 bronze badges 2
  • Is the pop-up modal (the user can't click on anything else while it's open)? If so, you could place a full-page transparent div under the pop-up (and on top of everything else). Then add an onclick event handler to the transparent div to close the pop-up (similar to adding an onclick event handler to the body tag, but less likely to cause a conflict). You'd have to keep track of which pop-up is currently open, so you know which one to close. – Matt Coughlin Commented Dec 9, 2012 at 16:41
  • You should use an (invisible) overlay right beneath the popup (100%x100%), to catch any clicks outside of the popup. If you dont use this, then other elements might stop propagation of the event, and the solutions below will not always work then.. And adding your own jsFiddle would be helpful, because people dont always have time to integrate your situation with your code. – EricG Commented Dec 10, 2012 at 8:30
Add a ment  | 

4 Answers 4

Reset to default 4
$(document).click(function(event) {
    if ( $(event.target).closest(".myPopup").get(0) == null ) {         
    alert('clicked outside');           
    } else{
    alert('clicked inside');
    }
});

This works for me.

Watch this for webkit. CodePen.IO

#overlay {
  top: 0;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: red;
  z-index: 40;
}

With:

document.getElementById("overlay").addEventListener("click", closePopup, false );
document.getElementById("popup").addEventListener("click", stopPropagation, false );

Try:

document.onmousedown = function(){
    if(event.target.className != 'myPopup')
        document.getElementsByClassName('myPopup')[0].style.display = 'none';
}
$('body').click(function(){
   $(".myPopup").hide();
});

Edit:

Maybe you can wrap your code in this:

<body>
   <a href="javascript:closePopup(<?=$id_pic?>)" style="curser:default">
      // all your code
   </a>
</body>

本文标签: javascriptClose DIV popup clicking outside of itStack Overflow