[JavaScript]JS中常遇到的瀏覽器安全色問題和解決方案__Java

來源:互聯網
上載者:User

JS中常遇到的瀏覽器安全色問題和解決方案 今天整理了一下瀏覽器對JS的相容問題,希望能給你們帶來協助,我沒想到的地方請留言給我,我再加上;
常遇到的關於瀏覽器的寬高問題:


//以下均可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座標

event事件問題:


//event事件問題    document.onclick=function(ev){//GoogleFirefox的寫法,IE9以上支援,往下不支援;        var e=ev;        console.log(e);    }    document.onclick=function(){//Google和IE支援,Firefox不支援;        var e=event;        console.log(e);    }    document.onclick=function(ev){//相容寫法;        var e=ev||window.event;        var mouseX=e.clientX;//滑鼠X軸的座標        var mouseY=e.clientY;//滑鼠Y軸的座標    }

DOM節點相關的問題,我直接封裝了函數,以便隨時可以拿來使用:


//DOM節點相關,主要相容IE 6 7 8    function nextnode(obj){//擷取下一個兄弟節點        if (obj.nextElementSibling) {            return obj.nextElementSibling;        } else{            return obj.nextSibling;        };    }    function prenode(obj){//擷取上一個兄弟節點        if (obj.previousElementSibling) {            return obj.previousElementSibling;        } else{            return obj.previousSibling;        };    }    function firstnode(obj){//擷取第一個子節點        if (obj.firstElementChild) {            return obj.firstElementChild;//非IE678支援        } else{            return obj.firstChild;//IE678支援        };    }    function lastnode(obj){//擷取最後一個子節點        if (obj.lastElementChild) {            return obj.lastElementChild;//非IE678支援        } else{            return obj.lastChild;//IE678支援        };    }

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所有元素;     }

擷取元素的非行間樣式值:


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

設定監聽事件:


//設定監聽事件     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,傳參的時候注意不要重複加了        };    }

元素到瀏覽器邊緣的距離:


//在這裡加個元素到瀏覽器邊緣的距離,很實用    function offsetTL(obj){//擷取元素內容距離瀏覽器邊框的距離(含邊框)        var ofL=0,ofT=0;        while(obj){            ofL+=obj.offsetLeft+obj.clientLeft;            ofT+=obj.offsetTop+obj.clientTop;            obj=obj.offsetParent;        }        return{left:ofL,top:ofT};    }

阻止事件傳播:


//js阻止事件傳播,這裡使用click事件為例    document.onclick=function(e){        var e=e||window.event;        if (e.stopPropagation) {            e.stopPropagation();//W3C標準        }else{            e.cancelBubble=true;//IE....        }    }

阻止預設事件:


//js阻止預設事件    document.onclick=function(e){        var e=e||window.event;        if (e.preventDefault) {            e.preventDefault();//W3C標準        }else{            e.returnValue='false';//IE..        }    }

關於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;//滑鼠去的地方    }

滑鼠滾輪滾動事件:


//滑鼠滾輪事件    //Firefox中的滾輪事件    document.addEventListener("DOMMouseScroll",function(event){        alert(event.detail);//若前滾的話為 -3,後滾的話為 3    },false)    //非Firefox中的滾輪事件    document.onmousewheel=function(event){        alert(event.detail);//前滾:120,後滾:-120    }</

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.