學習筆記 Firefox瀏覽器兼容JS腳本
本文向大家描述一下Firefox瀏覽器兼容JS腳本問題,最近做了一個項目,遇到了FireFox和IE腳本不兼容的問題,為此從網(wǎng)上搜集了部分兼容的腳本,這里和大家分享一下,希望對你的學習有所幫助。
Firefox瀏覽器兼容JS腳本供參考
1.window.event兼容腳本
- functiongetEvent(){//獲取瀏覽器事件,同時兼容ie和ff的寫法
 - if(document.all)returnwindow.event;
 - func=getEvent.caller;
 - while(func!=null){
 - vararg0=func.arguments[0];
 - if(arg0){
 - if((arg0.constructor==Event arg0.constructor==MouseEvent)
 - (typeof(arg0)=="object"&&arg0.preventDefault&&arg0.stopPropagation)){
 - returnarg0;
 - }
 - }
 - funcfunc=func.caller;
 - }
 - returnnull;
 - }
 
每次用事件之前Firefox都需要用getEvent()獲取一下,否則就是空
2.屏蔽Form提交事件
- event.returnValue=false;//forIE
 - evt.preventDefault();//forfirefox
 
3.獲取事件源
- varsource=event.srcElement//IE
 - varsource=event.target//firefox
 
#p#4.添加事件兼容寫法
- functionaddEvent(oElement,sEvent,func){
 - if(oElement.attachEvent){
 - oElement.attachEvent(sEvent,func);
 - }
 - else{
 - sEventsEvent=sEvent.substring(2,sEvent.length);
 - oElement.addEventListener(sEvent,func,false);
 - }
 - }
 
用法:addEvent(window,"onload",Start);
5.Firefox注冊innerText寫法
- //注冊firefoxinnerText
 - HTMLElement.prototype.__defineGetter__("innerText",
 - function(){
 - varanyString="";
 - varchildS=this.childNodes;
 - for(vari=0;iif(childS[i].nodeType==1)
 - anyString+=childS[i].tagName=="BR"?'\n':childS[i].innerText;
 - elseif(childS[i].nodeType==3)
 - anyString+=childS[i].nodeValue;
 - }
 - returnanyString;
 - }
 - );
 - HTMLElement.prototype.__defineSetter__("innerText",
 - function(sText){
 - this.textContent=sText;
 - }
 - );
 
6.長度:FireFox長度必須加“px”,IE無所謂
7.父控件下的子控件:IE是“children”,F(xiàn)ireFox是“childNodes”
8.XmlHttp
在IE中,XmlHttp.send(content)方法的content可以為空,而firefox則不能為空,應該用send(""),否則會出現(xiàn)411錯誤
【編輯推薦】
- JS在IE和Firefox兼容性問題及解決方案
 - 兩種方案解決IE6、IE7、Firefox兼容問題
 - 深入探究DIV CSS布局中position屬性用法
 - CSS兼容IE6,IE7,Firefox的技巧
 - CSS display:block顯示布局錯亂解決方案
 















 
 
 





 
 
 
 