<!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>
<script src="jquery-1.12.1.js"></script>
<script>
$(function () {
$("#dv1").click(function () {
console.log($(this).attr("id"));
});
$("#dv2").click(function () {
console.log($(this).attr("id"));
});
$("#dv3").click(function () {
console.log($(this).attr("id"));
});
//阻止事件冒泡
document.getElementById("dv3").onclick = function (e) {
//IE 8 以外其他浏览器
e.stopPropagation();
//IE 8
window.event.cancelBubble = true;
console.log("阻止此事件冒泡")
};
//除此以外还可以直接在要执行的函数里“return false”,这中方法不仅可以阻止事件冒泡,连浏览器默认行为(比如点击超链接会跳转)都能一块阻止了
});
</script>
</head>
<body>
<div id="dv1">
<div id="dv2">
<div id="dv3">
</div>
</div>
</div>
</body>
</html>