zoukankan      html  css  js  c++  java
  • 代码-JS之IE+GOOGLE兼容函数

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
    
    <tbody>
    <script>
        //兼容各个浏览器的获取样式
        function getStyle(element, styleName){
            if(element.currentStyle){
                //说明是IE浏览器
                return element.currentStyle[styleName]; //变量不能用于对象的属性,所以这里必须使用方括号的形式
            }else{
                //说明是非IE浏览器
                return getComputedStyle(element)[styleName]; //变量不能用于对象的属性,所以这里必须使用方括号的形式
            }
        }
    </script>
    </tbody>
    
    <tbody>
    <script>
        var e = window.event||evt;  // 获取兼容各个浏览器的事件对象
        console.log(e.keyCode);
    </script>
    </tbody>
    
    <tbody>
    <script>
        //  兼容各个浏览器的查找下一个兄弟
        function next(current){
            var n = current.nextSibling;
            while( n ){
                //console.log(n);
                if(n.nodeType == 1){
                    return n;
                }
                n = n.nextSibling;
            }
        }
    </script>
    </tbody>
    
    <tbody>
    <script>
        //兼容各个浏览器的事件监听函数
        function addEvent(element, type, chuli){
            if(element.addEventListener){
                // IE8+ 浏览器
                element.addEventListener(type, chuli);
            }else{
                //IE 低版本浏览器
                element.attachEvent('on' + type, chuli);
            }
        }
        //兼容各个浏览器的移除事件监听的函数
        function removeEvent(element, type, chuli) {
            if(element.removeEventListener){
                element.removeEventListener(type, chuli);
            }else{
                element.detachEvent('on'+type, chuli);
            }
        }
    </script>
    </tbody>
    
    <tbody>
    <script>
        //兼容各个浏览器的阻止冒泡的函数
        function zuzhi(e){
            //标准浏览器使用   evt.stopPropagation(); //evt指的是事件对象
            //IE内核浏览器使用 window.event.cancelBubble = true; // window.event 事件对象
            if(e.stopPropagation){
                e.stopPropagation();
            }else{
                e.cancelBubble = true;
            }
        }
    </script>
    </tbody>
    
    <tbody>
    <script>
        //兼容各个浏览器的阻止标签默认行为的方法
        function zuzhi(e) {
            //标准浏览器:evt.preventDefault();
            //IE内核浏览器:window.event.returnValue = false;
            if(e.preventDefault){
                e.preventDefault();
            }else{
                e.returnValue = false;
            }
        }
    </script>
    </tbody>
    
    <tbody>
    <script>
        //兼容各个浏览器的设置元素透明度
        function setOpacity(ele, val){
            if(ele.filters){
                ele.style.filter = "alpha(opacity=" + val*100 + ")";
            }else{
                ele.style.opacity = val;
            }
        }
    </script>
    </tbody>
    
    </body>
    </html>
    
    Copyright [2018] by [羊驼可以吃吗] form [https://www.cnblogs.com/phpisfirst/]
  • 相关阅读:
    【转】Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
    使用cacti监控服务器
    Vsphere client 无法登陆VCenter 处理的方法
    ESXI主机打开shell后主机警告处理
    Kiwi Syslog server 日志服务器搭建
    Linux lamp环境编译安装
    tar.bz2解压
    安装 MYSQL exec: g++: not found 报错
    mysql 编译安装提示“checking for termcap functions library... configure: error: No curses/termcap library found”
    Linux mysql 数据库忘记root密码
  • 原文地址:https://www.cnblogs.com/phpisfirst/p/9819127.html
Copyright © 2011-2022 走看看