在工作中遇见了因jquery冒泡引起的问题,刚好总结一下
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>阻止事件冒泡</title>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<style type="text/css">
.first{
width:200px;
height:200px;
background: #333;
color:#fff;
}
.secound{
width:100px;
height:100px;
background: #eee;
margin:0 auto;
color:#000;
}
.three{
display: block;
background: #f0f;
margin:0 auto 50px;
}
</style>
</head>
<body>
<div class="first">
第一层
<div class="secound">
第二层
<span class="three">第三层</span>
</div>
</div>
</body>
<script type="text/javascript">
$(function(){
$(".three").bind("click",function(event){
alert("three被触发");
// return false;
event.stopPropagation();//这里return和event.stopPropagation()起到一样的效果
})
$(".secound").bind("click",function(event){
alert("secound被触发");
// return false;
event.stopPropagation();
})
$(".first").bind("click",function(event){
alert("first被触发");
// return false;
event.stopPropagation();
})
})
</script>
</html>