冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>html5</title>
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<style type="text/css">
.box{width: 400px;background: #eec;}
.test1{width: 200px;height: 300px;background-color: #ccc;margin-top: 20px}
.test2{width: 200px;height: 300px;background-color: #ccc;margin-top: 20px}
</style>
<script type="text/javascript">
$(function(){
$(".box").bind('click', function(event) {
alert(1)
});
$(".test1").click(function(){
alert(2)
event.stopPropagation(); // 阻止事件冒泡
})
})
</script>
</head>
<body>
<div class="box">
<div class="test1"></div>
<div class="test2"></div>
</div>
</body>
</html>