Firefox瀏覽器兼容Javascript腳本的方法
來源:程序員人生 發布時間:2014-03-09 19:27:44 閱讀次數:3907次
遇到了FireFox和IE腳本不兼容的問題
1.window.event兼容腳本
function getEvent(){ //獲取瀏覽器事件,同時兼容ie和ff的寫法 if(document.all) return window.event; func=getEvent.caller; while(func!=null){ var arg0=func.arguments[0]; if(arg0){ if((arg0.constructor==Event arg0.constructor ==MouseEvent) (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){ return arg0; } } func=func.caller; } return null; } |
每次用事件之前Firefox都需要用getEvent()獲取一下,否則就是空
2.屏蔽Form提交事件
event.returnValue=false;// for IE evt.preventDefault();//for firefox |
3.獲取事件源
var source=event.srcElement //IE var source=event.target //firefox |
4.添加事件兼容寫法
function addEvent(oElement,sEvent,func){ if (oElement.attachEvent){ oElement.attachEvent(sEvent,func); } else{ sEvent=sEvent.substring(2,sEvent.length); oElement.addEventListener(sEvent,func,false); } } |
用法:addEvent(window,"onload",Start);
5.Firefox注冊innerText寫法
//注冊firefox innerText HTMLElement.prototype.__defineGetter__("innerText", function(){ var anyString = ""; var childS = this.childNodes; for(var i=0; i if(childS[i].nodeType==1) anyString += childS[i].tagName=="BR" ? '' : childS[i].innerText; else if(childS[i].nodeType==3) anyString += childS[i].nodeValue; } return anyString; } ); HTMLElement.prototype.__defineSetter__("innerText", function(sText){ this.textContent=sText; } ); |
6.長度:FireFox長度必須加“px”,IE無所謂
7.父控件下的子控件:IE是“children”,FireFox是“childNodes”
8.XmlHttp
在IE中,XmlHttp.send(content)方法的content可以為空,而firefox則不能為空,應該用send(" "),否則會出現411錯誤。
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈