• 夺命雷公狗---javascript NO:15 事件冒泡3


    解决事件冒泡兼容性问题

    ① 在public.js函数库中创建stopBubble函数

    ② 判断浏览器类型

    //冒泡兼容性问题

    function stopBubble(event){
        if(window.event){
            //IE
            window.event.cancelBubble = true;
        }else{
            event.stopPropagation();
        }
    }

    ③ 在html页面测试stopBubble函数

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=’utf-8′>
    <title></title>
    <style type=”text/css”>
    div#div1{
    width:500px;
    height:500px;
    background:red;
    }
    div#div2{
    width:400px;
    height:400px;
    background:blue;
    }
    div#div3{
    width:300px;
    height:300px;
    background:yellow;
    }
    </style>
    <script src=”public.js”></script>
    <script>
    window.onload = function(){
    addEvent($(‘div1′),’click’,function(){
    alert(‘div1′);
    })
    addEvent($(‘div2′),’click’,function(){
    alert(‘div2′);
    //IE模型
    //window.event.cancelBubble = true;
    //w3c
    //event.stopPropagation();
    stopBubble(event);
    })
    addEvent($(‘div3′),’click’,function(){
    alert(‘div3′);
    //IE模型
    //window.event.cancelBubble = true;
    //w3c
    //event.stopPropagation();
    stopBubble(event);
    })
    }
    </script>
    </head>
    <body>
    <div id=”div1″>
    <div id=”div2″>
    <div id=’div3′></div>
    </div>
    </div>
    </body>
    </html>
  • 相关阅读:
    手机网络制式常识
    合并两个有序数组a和b到c
    N皇后问题
    数独求解
    ARM处理器模式
    ARM异常中断处理
    国风·召南·野有死麕
    八大排序算法
    1045 Favorite Color Stripe (最长不下降子序列 LIS 或最长公共子序列 LCS)
    1066 Root of AVL Tree (模拟AVL建树)
  • 原文地址:https://www.cnblogs.com/leigood/p/5031887.html
Copyright © 2020-2023  润新知