前端兼容问题

一 html兼容问题

二 css兼容问题

1 获取元素的非行间样式值:

//获取元素的非行间样式值
     function getStyle(object,oCss) {
             if (object.currentStyle) {
                 return object.currentStyle[oCss];//IE
             }else{
                 return getComputedStyle(object,null)[oCss];//除了IE
             }
     }

2 浏览器的宽高问题:

//以下均可console.log()实验
    var winW=document.body.clientWidth||document.docuemntElement.clientWidth;//网页可见区域宽
    var winH=document.body.clientHeight||document.docuemntElement.clientHeight;//网页可见区域宽
    //以上为不包括边框的宽高,如果是offsetWidth或者offsetHeight的话包括边框
    
    var winWW=document.body.scrollWidth||document.docuemntElement.scrollWidth;//整个网页的宽
    var winHH=document.body.scrollHeight||document.docuemntElement.scrollHeight;//整个网页的高

    var scrollHeight=document.body.scrollTop||document.docuemntElement.scrollTop;//网页被卷去的高
    var scrollLeft=document.body.scrollLeft||document.docuemntElement.scrollLeft;//网页左卷的距离

    var screenH=window.screen.height;//屏幕分辨率的高
    var screenW=window.screen.width;//屏幕分辨率的宽
    var screenX=window.screenLeft;//浏览器窗口相对于屏幕的x坐标(除了FireFox)
    var screenXX=window.screenX;//FireFox相对于屏幕的X坐标
    var screenY=window.screenTop;//浏览器窗口相对于屏幕的y坐标(除了FireFox)
    var screenYY=window.screenY;//FireFox相对于屏幕的y坐标

三 js兼容问题

1 阻止事件传播:

//js阻止事件传播,这里使用click事件为例
    document.onclick=function(e){
        var e=e||window.event;
        if (e.stopPropagation) {
            e.stopPropagation();//W3C标准
        }else{
            e.cancelBubble=true;//IE....
        }
    }

2 关于EVENT事件中的target:

//关于event事件中的target
    document.onmouseover=function(e){
        var e=e||window.event;
        var Target=e.target||e.srcElement;//获取target的兼容写法,后面的为IE
        var from=e.relatedTarget||e.formElement;//鼠标来的地方,同样后面的为IE...
        var to=e.relatedTarget||e.toElement;//鼠标去的地方
    }

3 阻止默认事件:

//js阻止默认事件
    document.onclick=function(e){
        var e=e||window.event;
        if (e.preventDefault) {
            e.preventDefault();//W3C标准
        }else{
            e.returnValue='false';//IE..
        }
    }

 4 事件监听

//设置监听事件
     function addEvent(obj,type,fn){//添加事件监听,三个参数分别为 对象、事件类型、事件处理函数,默认为false
        if (obj.addEventListener) {
            obj.addEventListener(type,fn,false);//非IE
        } else{
            obj.attachEvent('on'+type,fn);//ie,这里已经加上on,传参的时候注意不要重复加了
        };
    }
    function removeEvent(obj,type,fn){//删除事件监听
        if (obj.removeEventListener) {
            obj.removeEventListener(type,fn,false);//非IE
        } else{
            obj.detachEvent('on'+type,fn);//ie,这里已经加上on,传参的时候注意不要重复加了
        };
    }

5 document.getElementsByClassName问题:

//通过类名获取元素
    document.getElementsByClassName('');//IE 6 7 8不支持;

    //这里可以定义一个函数来解决兼容问题,当然别在这给我提jQuery...
    //第一个为全局获取类名,第二个为局部获取类名
    function byClass1(oClass){//全局获取,oClass为你想要查找的类名,没有“.”
        var tags=document.all?document.all:document.getElementsByTagName('*');
        var arr=[];
        for (var i = 0; i < tags.length; i++) {
            var reg=new RegExp('\\b'+oClass+'\\b','g');
            if (reg.test(tags[i].className)) {
                arr.push(tags[i]);
            };
        };
        return arr;//注意返回的也是数组,包含你传入的class所有元素;
    }

    function byClass2(parentID,oClass){//局部获取类名,parentID为你传入的父级ID
        var parent=document.getElementById(parentID);
        var tags=parent.all?parent.all:parent.getElementsByTagName('*');
        var arr=[];
        for (var i = 0; i < tags.length; i++) {
        var reg=new RegExp('\\b'+oClass+'\\b','g');
            if (reg.test(tags[i].className)) {
                arr.push(tags[i]);
            };
        };
        return arr;//注意返回的也是数组,包含你传入的class所有元素;
     }

6 window.location.href

问题说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location。

解决方法:使用 window.location 来代替 window.location.href。当然也可以考虑使用 location.replace()方法。

猜你喜欢

转载自blog.csdn.net/weixin_42265852/article/details/87855803