<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
#dv1{
width: 300px;
height: 200px;
background-color: red;
}
#dv2{
width: 250px;
height: 150px;
background-color: green;
}
#dv3{
width: 200px;
height: 100px;
background-color: blue;
}
</style>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
#dv1{
width: 300px;
height: 200px;
background-color: red;
}
#dv2{
width: 250px;
height: 150px;
background-color: green;
}
#dv3{
width: 200px;
height: 100px;
background-color: blue;
}
</style>
<script>
</script>
</head>
<body>
<div id="dv1">
<div id="dv2">
<div id="dv3"></div>
</div>
</div>
<script>
document.getElementById("dv1").onclick = function(){
console.log(this.id);
}
document.getElementById("dv2").onclick = function(){
console.log(this.id);
//阻止事件冒泡
window.event.cancelBubble = true;
};
//第二种阻止事件冒泡的方式
document.getElementById("dv3").onclick = function(){
console.log(this.id);
//阻止事件冒泡
e.stopPropagation();
console.log(e);
}
</script>
</html>
2.阻止事件冒泡的二种方式
第一种:window.event.cancelBubble = true;
第二种:e.stopPropagation();