自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

IE和Firefox下編寫Javascript的區(qū)別

開發(fā) 前端
本文向大家描述一下IE和Firefox下編寫Javascript的不同區(qū)別,除了css以外,IE和FF對Javascript寫法有時也不同,也是讓人頭痛的問題,這里分享一下常用的區(qū)別。

你對IE和Firefox下編寫Javascript的區(qū)別是否了解,這里和大家簡單分享一下,相信本文介紹一定會讓你有所收獲。

IE和Firefox下編寫Javascript的區(qū)別

除了css以外,IE和FF對Javascript寫法有時也不同,也是讓人頭痛的問題,下面介紹些常用的區(qū)別。其中IE代替InternetExplorer,以MF代替MozzilaFirefox。

1.document.form.item問題

(1)現(xiàn)有問題:
現(xiàn)有代碼中存在許多document.formName.item("itemName")這樣的語句,不能在MF下運行
(2)解決方法:
改用document.formName.elements["elementName"]
(3)其它
參見2

2.集合類對象問題

(1)現(xiàn)有問題:
現(xiàn)有代碼中許多集合類對象取用時使用(),IE能接受,MF不能。
(2)解決方法:
改用[]作為下標運算。如:document.forms("formName")改為document.forms["formName"]。
又如:document.getElementsByName("inputName")(1)改為document.getElementsByName("inputName")[1]
(3)其它

3.window.event

(1)現(xiàn)有問題:
使用window.event無法在MF上運行
(2)解決方法:
MF的event只能在事件發(fā)生的現(xiàn)場使用,此問題暫無法解決??梢赃@樣變通:
原代碼(可在IE中運行):
 

  1. <inputtypeinputtype="button"name="someButton"value="提交"onclick=""/> 
  2. ...  
  3. <scriptlanguagescriptlanguage="javascript"> 
  4. functiongotoSubmit(){  
  5. ...  
  6. alert(window.event);//usewindow.event  
  7. ...  
  8. }  
  9. script> 
  10.  

新代碼(可在IE和MF中運行):
 

  1. <inputtypeinputtype="button"name="someButton"value="提交"onclick=""/> 
  2. ...  
  3. <scriptlanguagescriptlanguage="javascript"> 
  4. functiongotoSubmit(evt){  
  5. evtevt=evt?evt:(window.event?window.event:null);  
  6. ...  
  7. alert(evt);//useevt  
  8. ...  
  9. }  
  10. script> 


此外,如果新代碼中第一行不改,與老代碼一樣的話(即gotoSubmit調(diào)用沒有給參數(shù)),則仍然只能在IE中運行,但不會出錯。所以,這種方案tpl部分仍與老代碼兼容。#p#

4.HTML對象的id作為對象名的問題

(1)現(xiàn)有問題
在IE中,HTML對象的ID可以作為document的下屬對象變量名直接使用。在MF中不能。
(2)解決方法
用getElementById("idName")代替idName作為對象變量使用。

5.用idName字符串取得對象的問題

(1)現(xiàn)有問題
在IE中,利用eval(idName)可以取得id為idName的HTML對象,在MF中不能。
(2)解決方法
用getElementById(idName)代替eval(idName)。

6.變量名與某HTML對象id相同的問題

(1)現(xiàn)有問題
在MF中,因為對象id不作為HTML對象的名稱,所以可以使用與HTML對象id相同的變量名,IE中不能。
(2)解決方法
在聲明變量時,一律加上var,以避免歧義,這樣在IE中亦可正常運行。
此外,最好不要取與HTML對象id相同的變量名,以減少錯誤。
(3)其它
參見問題4

7.event.x與event.y問題

(1)現(xiàn)有問題
在IE中,event對象有x,y屬性,MF中沒有。
(2)解決方法
在MF中,與event.x等效的是event.pageX。但event.pageXIE中沒有。
故采用event.clIEntX代替event.x。在IE中也有這個變量。
event.clIEntX與event.pageX有微妙的差別(當整個頁面有滾動條的時候),不過大多數(shù)時候是等效的。

如果要完全一樣,可以稍麻煩些:
mX=event.x?event.x:event.pageX;
然后用mX代替event.x
(3)其它
event.layerX在IE與MF中都有,具體意義有無差別尚未試驗。#p#

8.關(guān)于frame

(1)現(xiàn)有問題
在IE中可以用window.testFrame取得該frame,mf中不行
(2)解決方法
在frame的使用方面mf和IE的最主要的區(qū)別是:
如果在frame標簽中書寫了以下屬性:

那么IE可以通過id或者name訪問這個frame對應(yīng)的window對象
而mf只可以通過name來訪問這個frame對應(yīng)的window對象
例如如果上述frame標簽寫在最上層的window里面的htm里面,那么可以這樣訪問
IE:window.top.frameId或者window.top.frameName來訪問這個window對象
mf:只能這樣window.top.frameName來訪問這個window對象

另外,在mf和IE中都可以使用window.top.document.getElementById("frameId")來訪問frame標簽
并且可以通過window.top.document.getElementById("testFrame").src='xx.htm'來切換frame的內(nèi)容
也都可以通過window.top.frameName.location='xx.htm'來切換frame的內(nèi)容
關(guān)于frame和window的描述可以參見bbs的‘window與frame’文章
以及/test/Javascript/test_frame/目錄下面的測試
----adun2004.12.09修改

9.在mf中,自己定義的屬性必須getAttribute()取得

10.在mf中沒有parentElementparement.children而用
parentNodeparentNode.childNodes
childNodes的下標的含義在IE和MF中不同,MF使用DOM規(guī)范,childNodes中會插入空白文本節(jié)點。
一般可以通過node.getElementsByTagName()來回避這個問題。
當html中節(jié)點缺失時,IE和MF對parentNode的解釋不同,例如
 

  1. <form> 
  2. <table> 
  3. <input/> 
  4. table> 
  5. form> 
  6.  

MF中input.parentNode的值為form,而IE中input.parentNode的值為空節(jié)點

MF中節(jié)點沒有removeNode方法,必須使用如下方法node.parentNode.removeChild(node)

11.const問題

(1)現(xiàn)有問題:
在IE中不能使用const關(guān)鍵字。如constconstVar=32;在IE中這是語法錯誤。
(2)解決方法:
不使用const,以var代替。

12.body對象

MF的body在body標簽沒有被瀏覽器完全讀入之前就存在,而IE則必須在body完全被讀入之后才存在。#p#

13.urlencoding

在Javascript中如果書寫url就直接寫&不要寫&例如varurl='xx.Javascriptp?objectName=xx&objectEvent=xxx';
frm.action=url那么很有可能url不會被正常顯示以至于參數(shù)沒有正確的傳到服務(wù)器
一般會服務(wù)器報錯參數(shù)沒有找到
當然如果是在tpl中例外,因為tpl中符合xml規(guī)范,要求&書寫為&
一般MF無法識別Javascript中的&


14.nodeName和tagName問題

(1)現(xiàn)有問題:
在MF中,所有節(jié)點均有nodeName值,但textNode沒有tagName值。在IE中,nodeName的使用好象
有問題(具體情況沒有測試,但我的IE已經(jīng)死了好幾次)。
(2)解決方法:
使用tagName,但應(yīng)檢測其是否為空。

15.元素屬性

IE下input.type屬性為只讀,但是MF下可以修改


16.document.getElementsByName()和document.all[name]的問題

(1)現(xiàn)有問題:

在IE中,getElementsByName()、document.all[name]均不能用來取得div元素(是否還有其它不能取的元素還不知道)。

1,document.getElementById替代document.all(IE適用)
2,集合[]替代()(IE適用)
3,target替代srcElement;parentNode替代parentElement(parentNodeIE適用)
4,node.parentNode.removeChild(node)替代removeNode(this)(IE適用)
5,有空白文本節(jié)點
6,無outerHTML屬性
7,事件局部變量e替代事件全局變量event
8,e.button鍵值有別于event.button,只有3個鍵值而無組合鍵值
9,無ondrag事件
10,DOMMouseScroll替代onmousewheel;-e.detail替代event.wheelDelta
11,addEventListener替代attachEvent;removeEventListener替代detachEvent
12,e.preventDefault()替代event.returnValue=false;e.stopPropagation()替代event.cancelBubble=true
13,style.top、style.left等嚴格檢查"px"單位(加"px"IE適用)
14,style="-moz-opacity:0.9"替代style="filter:alpha(opacity=90)";無其它filter
15,style.cursor="pointer"替代style.cursor="hand"(IE適用)
16,title替代alt(IE適用)
17,狀態(tài)欄默認不可修改,需調(diào)整FF設(shè)置
18,內(nèi)置繪圖功能以canvas或者SVG替代vml
19,代碼出錯時經(jīng)常不報錯(想來也是FF的無奈之舉吧,如果每個IE獨有的表達方式換在它里面都報錯的話,怕是報都報不過來吧)
20,對緩存的清理非常不好
注:標明“IE適用”者為通用性建議寫法,未標明者在IE里不適用。#p#

以下所有IE指IE6.0

驗證是否是IE瀏覽器(來之于googleJavascript)

varagt=navigator.userAgent.toLowerCase();
varis_IE=(agt.indexOf("msIE")!=-1&&document.all);
正式開始                                                    

◆事件委托方法

IE

document.body.onload=inject;//Functioninject()在這之前已被實現(xiàn)

Firefox

document.body.onload=inject();

有人說標準是:

document.body.onload=newFunction('inject()');

在Firefox無法取得event.srcElement

◆通過其他方式傳遞對象 

  1. if(isIE)  
  2. thistable.attachEvent("onmousedown",OnClickChangeTdBackColor);  
  3. //thistable.onmousedown=OnClickChangeTdBackColor;  
  4. else//dealFirefox  
  5. {  
  6.  
  7.  
  8. for(vari=0;i<thistable.rows.length;i++)  
  9. {  
  10. varrowObj=thistable.rows[i];  
  11. for(varj=0;j<rowObj.cells.length;j++)  
  12. {  
  13. varcellObj=rowObj.cells[j];  
  14. cellObj.setAttribute("onmousedown","OnClickChangeTdBackColor(this)");  
  15. }  
  16. //alert(rowObj.cells[0].tagName);  
  17. }  
  18. }  
  19.  

 ◆在Firefox下編寫事件處理函數(shù)

因為Firefox并沒有window.event.如果要得到event對象,就必須要聲明時間處理函數(shù)的第一個參數(shù)為event.

所以為了兼容IE與Firefox,一般的事件處理方法為:
 

  1. btn.onclick=handle_btn_click;  
  2. functionhandle_btn_click(evt)  
  3. {  
  4. if(evt==null)evt=window.event;//IE  
  5. //處理事件.  
  6. }  
  7.  

對于簡單的程序,這不算麻煩.

但對于一些復雜的程序,某寫函數(shù)根本就不是直接與事件掛鉤的.如果要把event傳進該參數(shù),那么所有的方法都要把event傳來傳去..這簡直就是噩夢.#p#

◆下面介紹一個解決這個麻煩事的方法,與原理.

Javascriptcript中,函數(shù)的調(diào)用是有一個func.caller這個屬性的.
例如 

  1. functionA()  
  2. {  
  3. B();  
  4. }  
  5. functionB()  
  6. {  
  7. alert(B.caller);  
  8. }  
  9.  

如果B被A調(diào)用,那么B.caller就是A

◆另外,函數(shù)有一個arguments屬性.這個屬性可以遍歷函數(shù)當前執(zhí)行的參數(shù):
 

  1. functionmyalert()  
  2. {  
  3. vararr=[];  
  4. for(vari=0;i  
  5. arr[i]=myalert.arguments[i];  
  6. alert(arr.join("-"));  
  7. }  
  8. alert("hello","world",1,2,3)  
  9.  

就能顯示hello-world-1-2-3
(arguments的個數(shù)與調(diào)用方有關(guān),而與函數(shù)的參數(shù)定義沒有任何關(guān)系)

根據(jù)這兩個屬性,我們可以得到第一個函數(shù)的event對象:

  1. btn.onclick=handle_click;  
  2. functionhandle_click()  
  3. {  
  4. showcontent();  
  5. }  
  6. functionshowcontent()  
  7. {  
  8. varevt=SearchEvent();  
  9. if(evt&&evt.shiftKey)//如果是基于事件的調(diào)用,并且shift被按下  
  10. window.open(global_helpurl);  
  11. else  
  12. location.href=global_helpurl;  
  13. }  
  14. functionSearchEvent()  
  15. {  
  16. func=SearchEvent.caller;  
  17. while(func!=null)  
  18. {  
  19. vararg0=func.arguments[0];  
  20. if(arg0)  
  21. {  
  22. if(arg0.constructor==Event)//如果就是event對象  
  23. returnarg0;  
  24. }  
  25. funcfunc=func.caller;  
  26. }  
  27. returnnull;  
  28. }  
  29.  

這個例子使用了SearchEvent來搜索event對象.其中'Event'是Firefox的event.constructor.
在該例子運行時,
SearchEvent.caller就是showcontent,但是showcontent.arguments[0]是空.所以func=func.caller時,func變?yōu)閔andle_click.
handle_click被Firefox調(diào)用,雖然沒有定義參數(shù),但是被調(diào)用時,第一個參數(shù)就是event,所以handle_click.arguments[0]就是event!

針對上面的知識,我們可以結(jié)合prototype.__defineGetter__來實現(xiàn)window.event在Firefox下的實現(xiàn):#p#

◆下面給出一個簡單的代碼..有興趣的可以補充 

  1. if(window.addEventListener)  
  2. {  
  3. FixPrototypeForGecko();  
  4. }  
  5. functionFixPrototypeForGecko()  
  6. {  
  7. HTMLElement.prototype.__defineGetter__("runtimeStyle",element_prototype_get_runtimeStyle);  
  8. window.constructor.prototype.__defineGetter__("event",window_prototype_get_event);  
  9. Event.prototype.__defineGetter__("srcElement",event_prototype_get_srcElement);  
  10. }  
  11. functionelement_prototype_get_runtimeStyle()  
  12. {  
  13. //returnstyleinstead...  
  14. returnthis.style;  
  15. }  
  16. functionwindow_prototype_get_event()  
  17. {  
  18. returnSearchEvent();  
  19. }  
  20. functionevent_prototype_get_srcElement()  
  21. {  
  22. returnthis.target;  
  23. }  
  24.  
  25. functionSearchEvent()  
  26. {  
  27. //IE  
  28. if(document.all)  
  29. returnwindow.event;  
  30.  
  31. func=SearchEvent.caller;  
  32. while(func!=null)  
  33. {  
  34. vararg0=func.arguments[0];  
  35. if(arg0)  
  36. {  
  37. if(arg0.constructor==Event)  
  38. returnarg0;  
  39. }  
  40. funcfunc=func.caller;  
  41. }  
  42. returnnull;  
  43. }  
  44. body>html> 
  45.  

 ◆Firefox與IE(parentElement)的父元素的區(qū)別

因為Firefox與IE都支持DOM,因此使用obj.parentNode是不錯選擇.

IE
obj.parentElement
Firefox
obj.parentNode

◆asp.net中UniqueID和clIEntID的區(qū)別

要使用document.getElementById方法,則使用控件的時候要這樣來作

"javascript:OnSelectSubCatalog(\""+subCatalog_drp.ClIEntID+"\","+catalogIDX+","+catID.ToString()+")";

◆調(diào)用Select元素的區(qū)別

移出一個選擇項

IE:sel.options.remove(sel.selectedIndex);
Firefox:

增加選擇項:

IE:subCatalog.add(newOption(text,value));

Firefox:
 

  1. varopnObj=document.createElement("OPTION");  
  2. //opnObj.id=optionID;  
  3. opnObj.value=value;  
  4. opnObj.text=text;  
  5. subCatalog.appendChild(opnObj);  
  6.  

cursor:handVScursor:pointer

Firefox不支持hand,但IE支持pointer,所以建議統(tǒng)一使用pointer。

文章來源:Div-Css.net設(shè)計網(wǎng)參考:http://www.div-css.net/div_css/topic/index.asp?id=7163

【編輯推薦】

  1. 探究IE和Firefox在JavaScript方面的兼容性
  2. IE7和IE8的CSS樣式八大差異
  3. 探究IE和Firefox下的2款HTTP調(diào)試工具用法
  4. Firefox和IE瀏覽器清除緩存方法揭秘
  5. 實現(xiàn)IE6、IE7、IE8多版本瀏覽器共存的五種方法
責任編輯:佚名 來源: div-css.net
相關(guān)推薦

2010-08-27 15:56:52

IEFirefoxCSS

2009-06-09 21:46:18

JavaScript差IEFirefox

2010-08-20 14:47:02

IEFirefoxCSS

2010-08-17 15:21:17

IEFirefoxHTML

2010-08-17 15:52:59

FirefoxIEJavaScript

2010-08-23 09:23:48

IEFirefox兼容性

2009-06-10 21:55:39

打開JavaScripIEFirefox

2010-09-15 09:21:11

IEirefoxJavascript

2010-10-09 12:43:20

JSfirefox

2010-08-23 16:09:31

IEfirefoxPadding

2010-08-20 13:02:09

IEFirefox

2010-08-20 13:22:21

IEFirefoxJavaScript

2010-08-18 13:23:36

FirefoxHTML

2010-08-20 10:12:14

IEFirefox

2010-08-16 14:42:15

DIV

2010-09-16 13:33:47

IE6IE7IE8

2010-08-18 15:41:38

IE6E7Firefox

2010-08-31 09:24:29

FireFoxIECSS

2010-08-19 10:56:55

JSIE6IE7

2010-04-26 10:39:56

IEFirefox安全
點贊
收藏

51CTO技術(shù)棧公眾號