关于JQuery的事件冒泡处理

  最近在写前端脚本的时候,总是会触发一个事件,从而导致其父元素或者其前代元素的事件也依次触发,弄了老半天,没弄好,后来才知道原来本身就自带处理这一问题的机制。

利用这行代码event.stopPropagation();就可以对其他的冒泡事件有一个限制。

 

以下是自己写的一串简单测试代码

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script type="text/ecmascript" src="jquery-1.11.3.min.js"></script>
<script type="text/ecmascript" src="JQuery.md5.js"></script>
<style>
*{
margin: 0px auto;
padding:0px;
}
.head{
width:300px;
height:200px;
border:1px solid;
}
.content{
width:50px;
height:50px;
}
</style>
</head>

<body>
<div class="head">
<div class="content">i am here</div>
</div>
<script type="text/javascript">
$(".head").bind("click",function(event){
alert(123);
event.stopPropagation();
});
$(".content").bind("click",function(event){
alert(456);
event.stopPropagation();
})
</script>
</body>
</html>

posted on 2015-10-14 10:05  随风丶飘零  阅读(218)  评论(0编辑  收藏  举报