阻止冒泡
Tutorial: DOM操作
Category: JS
Published: 2026-04-07 13:58:26
Views: 20
Likes: 0
Comments: 0
阻止冒泡事件
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.father {
overflow: hidden;
width: 300px;
height: 300px;
margin: 100px auto;
background-color: pink;
text-align: center;
}
.son {
width: 200px;
height: 200px;
margin: 50px;
background-color: purple;
line-height: 200px;
color: #fff;
}
</style>
</head>
<body>
<div class="father">
<div class="son">son儿子</div>
</div>
<script>
var son = document.querySelector(".son");
var father = document.querySelector(".father");
son.addEventListener(
"click",
function (e) {
alert("son");
e.stopPropagation();
},
false
);
father.addEventListener(
"click",
function () {
alert("father");
},
false
);
document.addEventListener("click", function () {
alert("document");
});
</script>
</body>
</html>