jquery 阻止冒泡事件和阻止默认事件

 jQuery 冒泡和默认事件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.js"></script>
    <style>
        div{
            padding:20px;
            border:1px solid gray;
        }
        .box1{
            position:relative;
            width:200px;
            margin:50px auto;
            background: red;
        }
        .box2{
             background: green;
        }
        .box3{
             background: blue;
        }
        .source{
            color:white;
        }
    </style>
    <script>
        $(function(){
            $('.box1').on('click',function(){
                alert(1);
            });
             $('.box2').on('click',function(){
                alert(2);
            });
             $('.box3').on('click',function(){
                alert(3);
            });
             $('.source').on('click',function(){
                alert("Hello,huanying2015!");
            });

        });
    </script>
</head>
<body>
    <div class="box1">
        <div class="box2">
            <div class="box3">
                <a href="http://www.baidu.com" class="source" target="_blank">触发源</a>  
            </div>
        </div>
    </div>
</body>
</html>

运行结果:

在这里:

弹出3,弹出2,弹出1   这三个事件是冒泡事件;

页面跳转到  www.baidu.com  为默认事件

分别可以使用不同的方法来阻止这两种事件的发生

1.阻止冒泡事件,使用 stopPropagation() 函数来执行

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.js"></script>
 7     <style>
 8         div{
 9             padding:20px;
10             border:1px solid gray;
11         }
12         .box1{
13             position:relative;
14             width:200px;
15             margin:50px auto;
16             background: red;
17         }
18         .box2{
19              background: green;
20         }
21         .box3{
22              background: blue;
23         }
24         .source{
25             color:white;
26         }
27     </style>
28     <script>
29         $(function(){
30             $('.box1').on('click',function(){
31                 alert(1);
32             });
33              $('.box2').on('click',function(){
34                 alert(2);
35             });
36              $('.box3').on('click',function(){
37                 alert(3);
38             });
39              $('.source').on('click',function( event ){
40                 alert("Hello,huanying2015!");
41                 event.stopPropagation();
42             });
43 
44         });
45     </script>
46 </head>
47 <body>
48     <div class="box1">
49         <div class="box2">
50             <div class="box3">
51                 <a href="http://www.baidu.com" class="source" target="_blank">触发源</a>  
52             </div>
53         </div>
54     </div>
55 </body>
56 </html>

运行结果:不会产生冒泡事件

 2.阻止默认事件:

使用 preventDefault() 来执行; 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.js"></script>
 7     <style>
 8         div{
 9             padding:20px;
10             border:1px solid gray;
11         }
12         .box1{
13             position:relative;
14             width:200px;
15             margin:50px auto;
16             background: red;
17         }
18         .box2{
19              background: green;
20         }
21         .box3{
22              background: blue;
23         }
24         .source{
25             color:white;
26         }
27     </style>
28     <script>
29         $(function(){
30             $('.box1').on('click',function(){
31                 alert(1);
32             });
33              $('.box2').on('click',function(){
34                 alert(2);
35             });
36              $('.box3').on('click',function(){
37                 alert(3);
38             });
39              $('.source').on('click',function(event){
40                 alert("Hello,huanying2015!");
41                 event.preventDefault();
42             });
43 
44         });
45     </script>
46 </head>
47 <body>
48     <div class="box1">
49         <div class="box2">
50             <div class="box3">
51                 <a href="http://www.baidu.com" class="source" target="_blank">触发源</a>  
52             </div>
53         </div>
54     </div>
55 </body>
56 </html>

 

运行结果:只会产生冒泡事件,不会执行默认跳转

 3.  如果既要阻止冒泡事件,又要阻止默认事件,改怎么做呢?

3.1 把阻止默认事件和冒泡事件合并起来,即如下:

3.2 使用 return false 来阻止

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6     <script src="http://apps.bdimg.com/libs/jquery/1.7.2/jquery.js"></script>
 7     <style>
 8         div{
 9             padding:20px;
10             border:1px solid gray;
11         }
12         .box1{
13             position:relative;
14             width:200px;
15             margin:50px auto;
16             background: red;
17         }
18         .box2{
19              background: green;
20         }
21         .box3{
22              background: blue;
23         }
24         .source{
25             color:white;
26         }
27     </style>
28     <script>
29         $(function(){
30             $('.box1').on('click',function(){
31                 alert(1);
32             });
33              $('.box2').on('click',function(){
34                 alert(2);
35             });
36              $('.box3').on('click',function(){
37                 alert(3);
38             });
39              $('.source').on('click',function(){
40                 alert("Hello,huanying2015!");
41                 return false;
42             });
43 
44         });
45     </script>
46 </head>
47 <body>
48     <div class="box1">
49         <div class="box2">
50             <div class="box3">
51                 <a href="http://www.baidu.com" class="source" target="_blank">触发源</a>  
52             </div>
53         </div>
54     </div>
55 </body>
56 </html>

运行结果:不会产生冒泡事件,也不会产生默认事件

 

转载于:https://www.cnblogs.com/huanying2015/p/7955219.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值