探究IE和Firefox在JavaScript方面的兼容性
你對IE和Firefox在JavaScript方面的兼容性問題是否了解,這里和大家簡單分享一下,相信本文介紹一定會(huì)讓你有所收獲。
IE和Firefox在JavaScript方面的兼容性
1.document.formName.item("itemName")問題
說明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];Firefox下,只能使用document.formName.elements["elementName"].
解決方法:統(tǒng)一使用document.formName.elements["elementName"].Text1:
- document.formName.item("itemName")
- document.formName.elements["elementName"]
2.集合類對象問題
說明:IE下,可以使用()或[]獲取集合類對象;Firefox下,只能使用[]獲取集合類對象.
解決方法:統(tǒng)一使用[]獲取集合類對象.
Text2:
- document.forms("formName")
- document.forms["formName"]
Text3:
- document.getElementsByName("inputName")(0)
- document.getElementsByName("inputName")[0]
3.自定義屬性問題
說明:IE下,可以使用獲取常規(guī)屬性的方法來獲取自定義屬性,也可以使用getAttribute()獲取自定義屬性;Firefox下,只能使用getAttribute()獲取自定義屬性.
解決方法:統(tǒng)一通過getAttribute()獲取自定義屬性.
Text4:
直接獲取自定義屬性的值
通過getAttribute()獲取自定義屬性的值
4.eval("idName")問題
說明:IE下,,可以使用eval("idName")或getElementById("idName")來取得id為idName的HTML對象;Firefox下只能使用getElementById("idName")來取得id為idName的HTML對象.
解決方法:統(tǒng)一用getElementById("idName")來取得id為idName的HTML對象.
Text5:
- eval("idName")
- document.getElementById("itemId")
5.變量名與某HTML對象ID相同的問題
說明:IE下,HTML對象的ID可以作為document的下屬對象變量名直接使用;Firefox下則不能.Firefox下,可以使用與HTML對象ID相同的變量名;IE下則不能。
解決方法:使用document.getElementById("idName")代替document.idName.***不要取HTML對象ID相同的變量名,以減少錯(cuò)誤;在聲明變量時(shí),一律加上var,以避免歧義.#p#
6.const問題
說明:Firefox下,可以使用const關(guān)鍵字或var關(guān)鍵字來定義常量;IE下,只能使用var關(guān)鍵字來定義常量.
解決方法:統(tǒng)一使用var關(guān)鍵字來定義常量.
7.input.type屬性問題
說明:IE下input.type屬性為只讀;但是Firefox下input.type屬性為讀寫.
8.window.event問題
說明:window.event只能在IE下運(yùn)行,而不能在Firefox下運(yùn)行,這是因?yàn)镕irefox的event只能在事件發(fā)生的現(xiàn)場使用.
解決方法:
IE:
- <inputnameinputname="Button8_1"type="button"value="IE"
- onclick="javascript:gotoSubmit8_1()"/>
- ...
- <scriptlanguagescriptlanguage="javascript">
- functiongotoSubmit8_1(){
- ...
- alert(window.event);//usewindow.event
- ...
- }
- script>
- IE&Firefox:
- <inputnameinputname="Button8_2"type="button"value="IE"
- onclick="javascript:gotoSubmit8_2(event)"/>
- ...
- <scriptlanguagescriptlanguage="javascript">
- functiongotoSubmit8_2(evt){
- ...
- evtevt=evt?evt:(window.event?window.event:null);
- alert(evt);//useevt
- ...
- }
- script>
9.event.x與event.y問題
說明:IE下,even對象有x,y屬性,但是沒有pageX,pageY屬性;Firefox下,even對象有pageX,pageY屬性,但是沒有x,y屬性.
解決方法:使用mX(mX=event.x?event.x:event.pageX;)來代替IE下的event.x或者Firefox下的event.pageX.
10.event.srcElement問題
說明:IE下,even對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,但是沒有srcElement屬性.
解決方法:使用obj(obj=event.srcElement?event.srcElement:event.target;)來代替IE下的event.srcElement或者Firefox下的event.target。#p#
11.window.location.href問題
說明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location.
解決方法:使用window.location來代替window.location.href.
12.模態(tài)和非模態(tài)窗口問題
說明:IE下,可以通過showModalDialog和showModelessDialog打開模態(tài)和非模態(tài)窗口;Firefox下則不能.
解決方法:直接使用window.open(pageURL,name,parameters)方式打開新窗口。
如果需要將子窗口中的參數(shù)傳遞回父窗口,可以在子窗口中使用window.opener來訪問父窗口.例如:varparWin=window.opener;parWin.document.getElementById("Aqing").value="Aqing";
13.frame問題
以下面的frame為例:
- <framesrcframesrc="xxx.html"id="frameId"name="frameName"/>
(1)訪問frame對象:
IE:使用window.frameId或者window.frameName來訪問這個(gè)frame對象.
Firefox:只能使用window.frameName來訪問這個(gè)frame對象.
另外,在IE和Firefox中都可以使用window.document.getElementById("frameId")來訪問這個(gè)frame對象.
(2)切換frame內(nèi)容:
在IE和Firefox中都可以使用window.document.getElementById("testFrame").src="xxx.html"或window.frameName.location="xxx.html"來切換frame的內(nèi)容.
如果需要將frame中的參數(shù)傳回父窗口,可以在frme中使用parent來訪問父窗口。例如:parent.document.form1.filename.value="Aqing";
14.body問題
Firefox的body在body標(biāo)簽沒有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標(biāo)簽被瀏覽器完全讀入之后才存在.
例如:
Firefox:
- <body>
- <scripttypescripttype="text/javascript">
- document.body.onclick=function(evt){
- evtevt=evt||window.event;
- alert(evt);
- }
- script>
- body>
- IE&Firefox:
- <body>
- body>
- <scripttypescripttype="text/javascript">
- document.body.onclick=function(evt){
- evtevt=evt||window.event;
- alert(evt);
- }
- script>
本篇文章來自IDC專家網(wǎng)原文鏈接:http://www.soidc.net/articles/1183730975605/20070807/1213992439909_1.html
【編輯推薦】