IE和Firefox在JavaScript方面的相容性)

來源:互聯網
上載者:User
IE和Firefox在JavaScript方面的相容性2008-01-05 13:15

1.document.formName.item("itemName") 問題
說明:
IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];
Firefox下,只能使用document.formName.elements["elementName"].

解決方案:
統一使用document.formName.elements["elementName"].
Text1:
document.formName.item("itemName")
document.formName.elements["elementName"]

-------------------------------------------------
2.集合類對象問題
說明:
IE下,可以使用()或[]擷取集合類對象;
Firefox下,只能使用[]擷取集合類對象.
解決方案:
統一使用[]擷取集合類對象.
Text2:
document.forms("formName")
document.forms["formName"]
Text3:
document.getElementsByName("inputName")(0)
document.getElementsByName("inputName")[0]

-------------------------------------------------
3.自訂屬性問題
說明:
IE下,可以使用擷取常規屬性的方法來擷取自訂屬性,也可以使用getAttribute()擷取自訂屬性;
Firefox下,只能使用getAttribute()擷取自訂屬性.
解決方案:
統一通過getAttribute()擷取自訂屬性.
Text4:
直接擷取自訂屬性的值
通過getAttribute()擷取自訂屬性的值

-------------------------------------------------
4.eval("idName")問題
說明:
IE下,可以使用eval("idName")或getElementById("idName")來取得id為idName的HTML對象;
Firefox下只能使用getElementById("idName")來取得id為idName的HTML對象.
解決方案:
統一用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相同的變數名,以減少錯誤;在聲明變數時,一律加上var,以避免歧義.

-------------------------------------------------
6.const問題
說明:
Firefox下,可以使用const關鍵字或var關鍵字來定義常量;
IE下,只能使用var關鍵字來定義常量.
解決方案:
統一使用var關鍵字來定義常量.

-------------------------------------------------
7.input.type屬性問題
說明:
IE下input.type屬性為唯讀;但是Firefox下input.type屬性為讀寫.

-------------------------------------------------
8.window.event問題
說明:
window.event只能在IE下運行,而不能在Firefox下運行,這是因為Firefox的event只能在事件發生的現場使用.
解決方案:
IE:
<input name="Button8_1" type="button" value="IE" />
...
<script language="javascript">
function gotoSubmit8_1() {
...
alert(window.event); //use window.event
...
}
</script>
IE&Firefox:
<input name="Button8_2" type="button" value="IE" />
...
<script language="javascript">
function gotoSubmit8_2(evt) {
...
evt=evt?evt:(window.event?window.event:null);
alert(evt); //use evt
...
}
</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.

-------------------------------------------------
11.window.location.href問題
說明:
IE或者Firefox2.0.x下,可以使用window.location或window.location.href;
Firefox1.5.x下,只能使用window.location.
解決方案
:使用window.location來代替window.location.href.

-------------------------------------------------
12.模態和非模態視窗問題
說明:
IE下,可以通過showModalDialog和showModelessDialog開啟模態和非模態視窗;
Firefox下則不能.
解決方案:
直接使用window.open(pageURL,name,parameters)方式開啟新視窗。
如果需要將子視窗中的參數傳遞迴父視窗,可以在子視窗中使用window.opener來訪問父視窗.
例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";

-------------------------------------------------
13.frame問題
以下面的frame為例:
<frame src="xxx.html" id="frameId" name="frameName" />
(1)訪問frame對象:
IE:使用window.frameId或者window.frameName來訪問這個frame對象.
Firefox:只能使用window.frameName來訪問這個frame對象.
另外,在IE和Firefox中都可以使用window.document.getElementById("frameId")來訪問這個frame對象.
(2)切換frame內容:
在IE和Firefox中都可以使用window.document.getElementById("testFrame").src = "xxx.html"或window.frameName.location = "xxx.html"來切換frame的內容.
如果需要將frame中的參數傳回父視窗,可以在frme中使用parent來訪問父視窗。例如:parent.document.form1.filename.value="Aqing";

-------------------------------------------------
14.body問題
Firefox的body在body標籤沒有被瀏覽器完全讀入之前就存在;
而IE的body則必須在body標籤被瀏覽器完全讀入之後才存在.
例如:
Firefox:
<body>
<script type="text/javascript">
document.body.onclick = function(evt){
evt = evt || window.event;
alert(evt);
}
</script>
</body>
IE&Firefox:
<body>
</body>
<script type="text/javascript">
document.body.onclick = function(evt){
evt = evt || window.event;
alert(evt);
}
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.