Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271966
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 21
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-20 14:44
文章分类

全部博文(124)

文章存档

2020年(1)

2018年(2)

2016年(2)

2015年(6)

2014年(10)

2013年(23)

2012年(7)

2011年(18)

2010年(15)

2009年(8)

2007年(8)

2006年(23)

2005年(1)

我的朋友

分类: JavaScript

2015-03-20 11:05:11


点击(此处)折叠或打开

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Insert title here</title>
  6. <script type="text/javascript" src="./js/jquery-1.11.1.js"></script>
  7. <script type="text/javascript">

  8. $(document).on("click",".test",function(event){
  9.     event.stopPropagation();
  10.     console.info("test is clicked");
  11. })
  12. $(document).ready(function(){
  13.     $("#hi").click(function(event){
  14.     //    event.stopPropagation(); //(1)
  15.         console.info("hi is clicked");
  16.     })
  17.     $("#hello").click(function(event){
  18.     //    event.stopPropagation(); //(2)
  19.         console.info("hello is clicked");
  20.     })
  21.     $("#hi").addClass("test");
  22. })

  23. </script>
  24. </head>
  25. <body>
  26. <div style="border:1px solid red;width:400px;height:400px;" id="hello">hello;
  27. <div style="border:1px solid green;width:200px;height:200px;margin:auto;" id="hi">hi;
  28. </div>
  29. </div>
  30. </body>
  31. </html>

代码的输出结果是:
 hi is clicked
hello is clicked
test is clicked

可见hi是原始方式绑定的,最先得到调用;
然后hello消息到达;
虽然hi被绑定两次,但是另一种是用document的中的class来绑定的所以消息最后到达;所以虽然test中有event.stop并不能阻止hello得到激活;相反hello中stop会阻止test的激活;
阅读(992) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~