最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 20:39:27
文檔

JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧

JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧:我們經(jīng)常在處理ie和firefox下的js總會碰到一些兼容問題,下面是些總結(jié),希望大家仔細(xì)看看研究1.document.formName.item("itemName") 問題說明:IE下,可以使用document.formName.item("itemName")或do
推薦度:
導(dǎo)讀JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧:我們經(jīng)常在處理ie和firefox下的js總會碰到一些兼容問題,下面是些總結(jié),希望大家仔細(xì)看看研究1.document.formName.item("itemName") 問題說明:IE下,可以使用document.formName.item("itemName")或do

我們經(jīng)常在處理ie和firefox下的js總會碰到一些兼容問題,下面是些總結(jié),希望大家仔細(xì)看看研究

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"].

2.集合類對象問題

說明:IE下,可以使用()或[]獲取集合類對象;Firefox下,只能使用[]獲取集合類對象.
解決方法:統(tǒng)一使用[]獲取集合類對象.

3.自定義屬性問題

說明:IE下,可以使用獲取常規(guī)屬性的方法來獲取自定義屬性,也可以使用getAttribute()獲取自定義屬性;Firefox下,只能使用getAttribute()獲取自定義屬性.
解決方法:統(tǒng)一通過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對象.

5.變量名與某HTML對象ID相同的問題

說明:IE下,HTML對象的ID可以作為document的下屬對象變量名直接使用;Firefox下則不能.Firefox下,可以使用與HTML對象ID相同的變量名;IE下則不能。
解決方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML對象ID相同的變量名,以減少錯誤;在聲明變量時(shí),一律加上var,以避免歧義.

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:
<input name="Button8_1" type="button" value="IE" onclick="javascript:gotoSubmit8_1()"/>
...
<script language="javascript">
function gotoSubmit8_1() {
...
alert(window.event); //use window.event
...
}
</script>
IE&Firefox:
<input name="Button8_2" type="button" value="IE" onclick="javascript:gotoSubmit8_2(event)"/>
...
<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.模態(tài)和非模態(tài)窗口問題

說明:IE下,可以通過showModalDialog和showModelessDialog打開模態(tài)和非模態(tài)窗口;Firefox下則不能.
解決方法:直接使用window.open(pageURL,name,parameters)方式打開新窗口。如果需要將子窗口中的參數(shù)傳遞回父窗口,可以在子窗口中使用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內(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>
<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>

15. 事件委托方法

IE:document.body.onload = inject; //Function inject()在這之前已被實(shí)現(xiàn)
Firefox:document.body.onload = inject();
有人說標(biāo)準(zhǔn)是:
document.body.onload=new Function('inject()');

16. firefox與IE(parentElement)的父元素的區(qū)別

IE:obj.parentElement
firefox:obj.parentNode
解決方法: 因?yàn)閒irefox與IE都支持DOM,因此使用obj.parentNode是不錯選擇.

17.cursor:hand VS cursor:pointer

firefox不支持hand,但ie支持pointer
解決方法: 統(tǒng)一使用pointer

18.innerText在IE中能正常工作,但是innerText在FireFox中卻不行.

解決方法:
if(navigator.appName.indexOf("Explorer") > -1){

document.getElementById('element').innerText = "my text";

} else{

document.getElementById('element').textContent = "my text";

}

19. FireFox中類似 obj.style.height = imgObj.height 的語句無效

解決方法:
obj.style.height = imgObj.height + 'px';

20. ie,firefox以及其它瀏覽器對于 table 標(biāo)簽的操作都各不相同,在ie中不允許對table和tr的innerHTML賦值,使用js增加一個tr時(shí),使用appendChile方法也不管用。

解決方法:
//向table追加一個空行:
var row = otable.insertRow(-1);
var cell = document.createElement("td");
cell.innerHTML = " ";
cell.className = "XXXX";
row.appendChild(cell);

21. padding 問題

padding 5px 4px 3px 1px FireFox無法解釋簡寫,必須改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;

22. 消除ul、ol等列表的縮進(jìn)時(shí)

樣式應(yīng)寫成:list-style:none;margin:0px;padding:0px;
其中margin屬性對IE有效,padding屬性對FireFox有效

23. CSS透明

IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:opacity:0.6。

24. CSS圓角

IE:不支持圓角。
FF: -moz-border-radius:4px,或者-moz-border-radius-topleft:4px;-moz-border- radius-topright:4px;-moz-border-radius-bottomleft:4px;-moz-border-radius- bottomright:4px;。

25. CSS雙線凹凸邊框

IE:border:2px outset;。
FF: -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080;

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧

JavaScript在IE和Firefox上的差異及相互替代的實(shí)現(xiàn)方法_javascript技巧:我們經(jīng)常在處理ie和firefox下的js總會碰到一些兼容問題,下面是些總結(jié),希望大家仔細(xì)看看研究1.document.formName.item("itemName") 問題說明:IE下,可以使用document.formName.item("itemName")或do
推薦度:
標(biāo)簽: IE firefox 差異
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top