天天看點

Javascript的IE和Firefox(火狐)相容性的常用例子

1.document.formname.item("itemname") 問題

說明:ie下,可以使用document.formname.item("itemname")或document.formname.elements["elementname"];

firefox下,隻能使用document.formname.elements["elementname"].

解決方法:統一使用document.formname.elements["elementname"].

2.集合類對象問題

說明:ie下,可以使用()或[]擷取集合類對象;firefox下,隻能使用[]擷取集合類對象.

解決方法:統一使用[]擷取集合類對象.

3.自定義屬性問題

說明:ie下,可以使用擷取正常屬性的方法來擷取自定義屬性,也可以使用getattribute()擷取自定義屬性;firefox下,隻能使用getattribute()擷取自定義屬性.

解決方法:統一通過getattribute()擷取自定義屬性.

4.eval("idname")問題

說明:ie下,,可以使用eval("idname")或getelementbyid("idname")來取得id為idname的html對象;firefox下隻能使用getelementbyid("idname")來取得id為idname的html對象.

解決方法:統一用getelementbyid("idname")來取得id為idname的html對象.

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隻能在事件發生的現場使用. firefox必須從源處加入event作參數傳遞。ie忽略該參數,用window.event來讀取該event。

解決方法:

ie&firefox:

submitted(event)"/> …

<script language="javascript">

function submitted(evt) {

evt=evt?evt:(window.event?window.event:null);

}

</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下,event對象有srcelement屬性,但是沒有target屬性;firefox下,even對象有target屬性,但是沒有srcelement屬性.

解決方法:使用obj(obj = event.srcelement ? event.srcelement : event.target;)來代替ie下的event.srcelement或者firefox下的event.target.  請同時注意event的相容性問題。

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對象. frameid和framename可以同名。

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中的參數傳回父視窗(注意不是opener,而是parent frame),可以在frme中使用parent來通路父視窗。例如:parent.document.form1.filename.value="aqing";

14.body問題

firefox的body在body标簽沒有被浏覽器完全讀入之前就存在;而ie的body則必須在body标簽被浏覽器完全讀入之後才存在.

15. 事件委托方法

ie:document.body.onload = inject; //function inject()在這之前已被實作

firefox:document.body.onload = inject();

16. firefox與ie的父元素(parentelement)的差別

ie:obj.parentelement

firefox:obj.parentnode

解決方法: 因為firefox與ie都支援dom,是以使用obj.parentnode是不錯選擇.

17.cursor:hand vs cursor:pointer

firefox不支援hand,但ie支援pointer

解決方法: 統一使用pointer

18.innertext在ie中能正常工作,但是innertext在firefox中卻不行. 需用textcontent。

if(navigator.appname.indexof("explorer") > -1){

    document.getelementbyid('element').innertext = "my text";

} else{

    document.getelementbyid('element').textcontent = "my text";

19. firefox中設定html标簽的style時,所有位置性和字型尺寸的值必須後跟px。這個ie也是支援的。

20. ie,firefox以及其它浏覽器對于 table 标簽的操作都各不相同,在ie中不允許對table和tr的innerhtml指派,使用js增加一個tr時,使用appendchild方法也不管用。

解決方法:

//向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等清單的縮進時

樣式應寫成: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;

26. 對select的options集合操作

枚舉元素除了[]外,selectname.options.item()也是可以的, 另外selectname.options.length, selectname.options.add/remove都可以在兩種浏覽器上使用。注意在add後指派元素,否則會失敗(本人試驗如此)。

27. xmlhttp的差別

//mf

if (window.xmlhttprequest) //mf

  {

  xmlhttp=new xmlhttprequest()

  xmlhttp.onreadystatechange=xmlhttpchange

  xmlhttp.open("get",url,true)

  xmlhttp.send(null)

  }

//ie

else if (window.activexobject)  // code for ie

  xmlhttp=new activexobject("microsoft.xmlhttp")

    if (xmlhttp)

    {

    xmlhttp.onreadystatechange=xmlhttpchange

    xmlhttp.open("get",url,true)

    xmlhttp.send()

    }

28. innerhtml的差別

firefox不支援innerhtml, 解決辦法可以如下

       rng = document.createrange();

       el = document.getelementbyid(elementid);

       rng.setstartbefore(el);

       htmlfrag = rng.createcontextualfragment(content);

       while (el.haschildnodes()) //清除原有内容,加入新内容

              el.removechild(el.lastchild);

       el.appendchild(htmlfrag);

繼續閱讀