zoukankan      html  css  js  c++  java
  • 夺命雷公狗jquery---26事件冒泡介绍和阻止方法

    什么是事件冒泡可以通过以下代码即可知晓

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <style type="text/css">
                #div1{
                    width:400px;
                    height:400px;
                    background-color:red;
                }
    
                #div2{
                    width:300px;
                    height:300px;
                    background-color:blue;
                }
    
                #div3{
                        width:200px;
                        height:200px;
                        background-color:green;
                }
            </style>
            <script src="js/jquery.js"></script>
            <script>
                $(function(){
                    $('#div1').bind('click',function(){
                        alert('div1');
                    });
                    
                    $('#div2').bind('click',function(){
                        alert('div2');
                    });
    
                    $('#div3').bind('click',function(){
                        alert('div3');
                    });
                })
            </script>
        </head>
        <body>
            <div id="div1">
                <div id="div2">
                    <div id="div3"></div>
                </div>
            </div>
        </body>
    </html>

    阻止的方法其实也很简单,只要加上event  和event.stopPropagetion()即可阻止事件冒泡了,如下代码所示

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <style type="text/css">
                #div1{
                    width:400px;
                    height:400px;
                    background-color:red;
                }
    
                #div2{
                    width:300px;
                    height:300px;
                    background-color:blue;
                }
    
                #div3{
                        width:200px;
                        height:200px;
                        background-color:green;
                }
            </style>
            <script src="js/jquery.js"></script>
            <script>
                $(function(){
                    $('#div1').bind('click',function(){
                        alert('div1');
                    });
                    
                    $('#div2').bind('click',function(event){
                        alert('div2');
                        event.stopPropagation();
                    });
    
                    $('#div3').bind('click',function(event){
                        alert('div3');
                        event.stopPropagation();
                    });
                })
            </script>
        </head>
        <body>
            <div id="div1">
                <div id="div2">
                    <div id="div3"></div>
                </div>
            </div>
        </body>
    </html>
  • 相关阅读:
    KindEditor的使用
    python过滤文件中特殊标签
    django中orm的简单操作
    django中models联合唯一unique_together
    博客当中的文章分类以及归档
    zabbix前端添加平台脚本监控
    django重写form表单中的局部钩子函数
    input获取、失去焦点对输入内容做验证
    django admin后台的简单使用
    django中博客后台将图片上传作为用户头像
  • 原文地址:https://www.cnblogs.com/leigood/p/4914324.html
Copyright © 2011-2022 走看看