事件冒泡
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件冒泡</title>
<style type="text/css">
div{padding: 50px;}
#div1{background: #1836EC;}
#div2{background: #D43434;}
#div3{background: #CFF6AE;}
</style>
</head>
<script>
window.onload=function (){
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
var oDiv3=document.getElementById('div3');
function fn(){
alert(this.id);
}
oDiv1.onclick=fn;
}
</script>
<body>
<div id="div1">1
<div id="div2">2
<div id="div3">3</div>
</div>
</div>
</body>
</html>
事件冒泡 下拉菜单应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件冒泡</title>
<style type="text/css">
div{width: 100px;height: 200px;border:1px solid #f00;display: none;}
</style>
</head>
<script type="text/javascript">
window.onload=function (){
oBtn=document.getElementsByTagName('button')[0];
oDiv=document.getElementsByTagName('div')[0];
oBtn.onclick=function (ev){
var ev = ev || event;
oDiv.style.display='block';
event.cancelBubble=true;
}
document.onclick=function (){
oDiv.style.display='none';
}
}
</script>
<body>
<button>展开</button>
<div></div>
<p>111111</p>
<p>111111</p>
<p>111111</p>
</body>
</html>
事件冒泡 分享菜单应用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件冒泡</title>
<style type="text/css">
body{margin:0;}
#div1{width: 200px;height: 300px;background: #f00;position: absolute;top: 100px;left: -200px;}
#div2{position: absolute;top: 0;right: -50px;width: 50px;height: 50px;background: #4714E5;color: #fff;}
</style>
</head>
<script type="text/javascript">
window.onload=function (){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function (){
oDiv.style.left=0;
}
oDiv.onmouseout=function (){
oDiv.style.left=-200+'px';
}
}
</script>
<body>
<div id="div1"><div id="div2">分享到</div></div>
</body>
</html>