標籤:window opp span mon document eve code val 滾動
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 }
節點載入:
//Firefox下特有的節點載入事件,就是節點載入完才執行,和onload不同 //感覺用到的不多,直接把js代碼放在頁面結構後面一樣能實現。。 document.addEventListener(‘DOMContentLoaded‘,function ( ){},false);//DOM載入完成。好像除IE6-8都可以.
轉載自http://www.cnblogs.com/duenyang/archive/2016/11/15/6066737.html
JS中常遇到的瀏覽器安全色問題和解決方案