Js click elsewhere on the page to close the pop up layer of sample code

  • 2020-03-30 01:01:50
  • OfStack


<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> Headless document </title> 
<style type="text/css"> 
<!-- 
*{font-size:12px;font-family:Verdana, Geneva, sans-serif;line-height:14px} 
a{color:#039} 
a:hover{color:#f60} 
.pop{position:absolute;left:40%;top:40%;width:300px;height:100px;background:#eee;border:1px solid #ccc} 
.pop_head{position:relative;height:20px;background:#ccc} 
.pop_head a{position:absolute;right:8px;line-height:20px;color:#000;text-decoration:none} 
.pop_head a:hover{color:#f60;text-decoration:none} 
.pop_body{padding:8px} 
--> 
</style> 
</head> 
<body> 
<!-- Start by setting up a layer :--> 
<div id="pop" class="pop" style="display:none" onclick="show(event,'pop');"> 
<div class="pop_head"><a href="javascript:void(0);" onclick="hide('pop')"> Shut down </a></div> 
<div class="pop_body"> Thank you for coming... </div> 
</div> 
<!-- Pop up the layer button :--> 
<a href="javascript:void(0);" onclick="show(event,'pop');"> The pop-up button </a> 
<script type="text/javascript"> 
var url = '#'; 
function show(evt,o){ 
evt.stopPropagation?evt.stopPropagation():evt.cancelBubble=true;
var o = document.getElementById(o); 
o.style.display = ""; 
} 
function hide(o){ 
var o = document.getElementById(o); 
o.style.display = "none"; 
window.location = url; 
} 
document.onclick=function(){hide('pop');}
</script> 
</body> 
</html>

Conclusion:

1. When the pop-up method is called, an event object is passed in: event.

2. Pop-up method to add event binding code.

3. There should be a global js code, which can be used to call the method to hide the pop-up layer when clicking on other parts.


Related articles: