您的位置:首页 > Web前端 > JavaScript

js中e.cancelBubble解决事件冒泡

2018-03-18 16:41 295 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#par{
width:400px;height:400px;background:white; border:1px solid red;
}
#son{width:200px;height:200px;background:rgb(200,0,0);}
</style>
</head>
<body>
<div id='par'>
<div id="son"></div>
</div>
</body>
<script>
var par = document.getElementById('par');
var son = document.getElementById('son');

son.onclick=function(e){
var e = e ||event;
alert('点击了par');
//阻止事件冒泡

// 标准浏览器有效 IE不行
//e.stopPropagation();

//没有兼容性问题阻止事件冒泡
e.cancelBubble = true;
}

par.onclick=function(){

alert('点击了son');

}

</script>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: