惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
Vercel News
Vercel News
T
The Blog of Author Tim Ferriss
F
Full Disclosure
A
About on SuperTechFans
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
Know Your Adversary
Know Your Adversary
K
Kaspersky official blog
L
LINUX DO - 热门话题
Recorded Future
Recorded Future
C
Cisco Blogs
M
MIT News - Artificial intelligence
T
Tenable Blog
G
GRAHAM CLULEY
月光博客
月光博客
Recent Announcements
Recent Announcements
V
Visual Studio Blog
IT之家
IT之家
T
The Exploit Database - CXSecurity.com
The GitHub Blog
The GitHub Blog
T
Threat Research - Cisco Blogs
D
DataBreaches.Net
P
Privacy International News Feed
P
Proofpoint News Feed
I
Intezer
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - Franky
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
Hacker News - Newest:
Hacker News - Newest: "LLM"
O
OpenAI News
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
N
News and Events Feed by Topic
The Cloudflare Blog
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
B
Blog RSS Feed

博客园 - db's jim

NuGet 无法连接到远程服务器-解决方法(转) 未能解析此远程名称:’nuget.org’ 解决方法(转) Asp.net中的认证与授权 JS的事件监听机制 用映射的方法获取当前方法的名称 log4net的各种Appender配置示例(转) System.Diagnostics命名空间里的Debug类和Trace类的用途(收藏) Newtonsoft.Json处理日期问题 Failed to execute request because the App-Domain could not be created C#Windows服务程序安装 VS2010快捷键 生成方法存根 (Stub) NHibernate 事务查询的更新事件 Nhibernate.hbm2ddl.auto配置详解 NHibernate.Tool.hbm2ddl SchemaExport SQL2005 开窗函数 通过WEB方式修改windows帐号的秘密 将EXCEL文档导入SQL server 2005错误 castle ActiveRecord 初始化
[转]jquery对事件冒泡的处理方法
db's jim · 2012-08-22 · via 博客园 - db's jim

原文地址:http://www.cnblogs.com/zhujie/archive/2011/02/11/1951533.html

1.什么是事件冒泡:

  页面上有好多事件,也可以多个元素响应一个事件.假如:

<BODY onclick="alert('aaa');">
<div onclick="alert('bbb');">
 <a href="#" class="cooltip" title="这是我的超链接提示1。" onclick="alert('ddd');">
   提示
  </a>
</div>
</BODY>

上面这段代码一共有三个事件,body,div,a都分别绑定了单击事件。在页面中当单击a标签会连续弹出3个提示框。这就是事件冒泡引起的现象。事件冒 泡的过程是:a --> div --> body 。a冒泡到div冒泡到body

2.事件冒泡引发的问题。

 本来在上面的代码中只想触发<a>元素的onclick事件,然而<div>,<body>事件也同时 触发了。因此我们必须要对事件的作用范围进行限制。当单击<a>元素的onclick事件时只触发<a>本身的事件。由于IE- DOM和标准DOM实现事件对象的方法各不相同,导致在不同浏览器中获取事件的对象变得比较困难。如果想阻止事件的传递,我们可以用 event.stopPropagation()阻止事件的传递行为.

3.jQuery对这个问题进行了必要的扩展和封装.

 $("element").bind("click",function(event){ 

     //event为事件对象

    //.........

   event.stopPropagation();   //停止事件冒泡

  });

4.阻止默认行为

 网页中的某些元素是有自己的默认行为的,比如果超链接单节后需要跳转,提交按钮点击后需要提交表单,有时需要阻止这些行为,也就是默认行为。

 jquery中可用用preventDefault()的方法来阻止元素的默认行为.

$('#submit').bind('click',function(event){

   var username = $('#username').val();

   if(username==""){

     alert('用户名不能为空!');

     event.preventDefault();   //阻止默认行为

   }

})

5.jquery中对冒泡和默认行为的阻止方法同样也可以改写,改写后能够达到同样的效果

event.preventDefault();  改写为:  return false;

event.stopPropagation();  改写为:  return false;

 PS:推荐大家不要图省事,任何时候都写return false;

一般情况下都是需要阻止默认行为。不要阻止事件冒泡。