<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div{
width: 500px;
height: 500px;
background: #ccc;
margin:100px auto;
display: none;
}
</style>
</head>
<body>
<button id="btn">点击</button>
<div id="box"></div>
</body>
</html>
<script>
var btn=document.getElementById('btn');
var box=document.getElementById('box');
btn.onclick=function(e){
box.style.display='block';
window.event? window.event.cancelBubble = true : e.stopPropagation();//阻止事件向上冒泡
}
document.onclick=function(e){
box.style.display='none';
}
box.onclick=function(e){
window.event? window.event.cancelBubble = true : e.stopPropagation();//阻止事件向上冒泡
}
</script>
点击盒子外任意位置关闭当前盒子
最新推荐文章于 2024-03-27 18:22:36 发布