首页 技术 正文
技术 2022年11月11日
0 收藏 726 点赞 2,375 浏览 1786 个字

JQuery中事件冒泡

定义

在一个对象上触发某类事件,就会执行此事件程序,如果没有处理事件就会向这个对象的父级对象传播 直至它被处理,最顶层老大为document对象。

作用

事件冒泡允许多个操作被集中处理(把事件处理器添加到一个父级元素上,避免把事件处理器添加到多个子级元素上)

阻止事件气泡

事件气泡有时是不需要的 通过event.stopPropagation() 或者event.preventDefault()组织 —-合并写法:return false;

解决思路

假如页面中有一个模块1,模块1中有又一个模块2,模块2中有个模块3,如果点击模块3中的触发事件,如没有处理程序,他就会向模块2执行,模块2没有继续向模块1执行,此为一个冒泡事件,如果想在模块3就终止需要—return false

具体实例代码

<!DOCTYPE html>
<html>
    <head>
        <meta charset=”UTF-8″>
        <title></title>
        <style>
            .father {width: 500px;height: 500px;background: pink;}
            .son {width: 250px;height: 250px;background: red; }
            .grandson {width: 150px;height: 150px; background: gold;}
        </style>
        <script type=”text/javascript” src=”js/jquery-1.12.4.min.js” ></script>
        <script>
                $(function(){
                            var $box1 = $(‘.father’);
                        var $box2 = $(‘.son’);
                           var $box3 = $(‘.grandson’);
                        $box1.click(function() {
                            alert(‘father’);
                                });
                                
                        $box2.click(function() {
                               alert(‘son’); return false;
                                });
                                
                        $box3.click(function(event) {
                               alert(‘grandson’);
                            return false;
                                });
//                         $(document).click(function(event) {
//                          alert(‘grandfather’);
//                              });
                             })    
        </script>
    </head>
    <body>
        <div class=”father”>
            <div class=”son”>
                <div class=”grandson”></div>
    </div>
</div>
    </body>
</html>

易错点

return false 写在函数内部

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289