• 事件冒泡


    // 事件冒泡
        // 当触发某个元素的某个事件时,它会先触发自己的对应事件,然后,依次向上触发所有父级的相同事件,如果中间有父级没有相同事件,继续向上触发
    
        // IE提出的
     var obox1 = document.querySelector(".box1")
        var obox2 = document.querySelector(".box2")
        var obox3 = document.querySelector(".box3")
    
        obox1.onclick = function(eve){
            var e = eve || window.event;  //e是谷歌中表示的时间,window.event是IE浏览器中表示的事件
            
            if(e.stopPropagation){          //表示在IE中清除了冒泡
                e.stopPropagation();
            }else{                         //表示在谷歌中清除了冒泡
                e.cancelBubble = true;
            }
            alert("red")
        }
    
        obox2.onclick = function(eve){
            var e = eve || window.event;
            if(e.stopPropagation){
                e.stopPropagation();
            }else{
                e.cancelBubble = true;
            }
            alert("blue")
        }
        
        obox3.onclick = function(eve){
            var e = eve || window.event;
            stopBubble(e);  //调用了封装的函数
            alert("green")
        }
    
        document.onclick = function(){
            alert(1)
        }
        
    
    兼容:(封装成一个函数) 为什么要设置兼容,因为ie和谷歌对事件的表示不同。一个是window.event,一个直接输出函数
        function stopBubble(e){
            if(e.stopPropagation){
                e.stopPropagation();
            }else{
                e.cancelBubble = true;
            }
        }
  • 相关阅读:
    Storm—Storm集群搭建
    fis3工程化中的模块化开发
    rem、px、em(手机端h5页面屏幕适配的几种方法)
    最完整的React+Redux+router兼容ie8 修改!!!!
    移动端常见的一些兼容性问题
    移动端常见问题及解决方案
    EditorConfig 介绍
    当当主页
    JD主页
    react 项目的一个ie8兼容性问题
  • 原文地址:https://www.cnblogs.com/hy96/p/11413864.html
Copyright © 2020-2023  润新知