天天看点

js中e.cancelBubble解决事件冒泡

<!DOCTYPE html>

<html >

<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.οnclick=function(e){

var e = e ||event;

alert('点击了par');

//阻止事件冒泡

// 标准浏览器有效 IE不行

//e.stopPropagation();

//没有兼容性问题阻止事件冒泡

e.cancelBubble = true;

}

par.οnclick=function(){

alert('点击了son');

}

</script>

</html>

继续阅读