码迷,mamicode.com
首页 > Web开发 > 详细

JQuery中event的preventDefault和stopPropagation介绍

时间:2014-12-14 10:33:46      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

event.preventDefault()阻止默认事件行为的触发。

event.stopPropagation()防止事件冒泡到DOM树上,也就是不触发的任何前辈元素上的事件处理函数。

bubuko.com,布布扣
 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title>取消form表单提交或a打开的超连接</title>
 6     <script src="/js/jquery-1.8.2.js"></script>
 7     <script language="javascript" type="text/javascript">
 8         $(function () {
 9             $("*").each(function (index, item) {
10                 $(item).bind("click", function (event) {
11                     output("事件源:" + event.target.id + "," + event.target.tagName + ",事件对象:" + event.currentTarget.id);//DOM2会产生冒泡
12                     //取消form表单提交或a打开的超连接
13                     event.preventDefault();
14                     //同样也支持取消事件冒泡
15                     //event.stopPropagation();
16                 });
17             });
18         });
19         function output(text) {
20             $("#content").append(text + "<br/>");
21         }
22     </script>
23 </head>
24 <body id="body">
25     <div id="parent">
26         <div id="child">
27             点击这里
28             <a id="link" href="http://www.baidu.com">超连接</a>
29             <form id="form" action="http://www.baidu.com">
30                 <input id="submit" type="submit" value="submit"/>
31             </form>
32         </div>
33     </div>
34     <div id="content">
35     </div>
36 </body>
37 </html>
jquery_event05

 

JQuery中event的preventDefault和stopPropagation介绍

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/qiyebao/p/4162222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!