javascript 利用冒泡机制显示与隐藏模态框

 <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
body,html {
height: 100%;
padding: 0;
margin: 0;
}
.mask {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
display: none;
background: rgba(0, 0, 0, 0.6);
}
.login {
width: 400px;
height: 300px;
cursor: pointer;
background-color: #fff;
margin: 200px auto;
}
</style>
</head>
<body>
<div class="mask">
<div class="login" id="login"></div>
</div>
<a href="#">注册</a>
<a href="#">登陆</a> <script src="animate.js" charset="utf-8"></script>
<script type="text/javascript">
var mask = document.getElementsByClassName("mask")[0];
var a = document.getElementsByTagName("a")[1]; a.onclick = function(event){
//显示模态框
show(mask);
//阻止冒泡
event = event || window.event;
if (event && event.stopPropagation) {
event.stopPropagation();
}else {
event.cancelBubble = true;
}
} document.onclick = function(event){
//获取点击按钮后传递过来的值。
event = event || window.event;
//兼容获取事件触动时,被传递过来的对象
var aaa = event.target ? event.target : event.srcElement;
//判断目标值的ID是否等于login,如果等于不隐藏盒子,否则隐藏盒子。
if (aaa.id !== "login") {
mask.style.display = "none";
}
}
</script> </body>
</html>
上一篇:使用ASP.Net WebAPI构建REST服务(六)——Self-Host


下一篇:zTree的调用设使用(跨两个系统,两类技术实现的项目案例SpringMVC+Spring+MyBatis和Struts2+Spring+ibatis框架组合)