firefox與IE對js和CSS的區別

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)"/> …

 
function Submitted(evt) { 
evt=evt?evt:(window.event?window.event:null); 
} 


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("C_Name").value = "john";


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),可以在frame中使用parent來訪問父窗口。例如:

parent.document.form1.filename.value="jquery";


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. 設置HTML標籤的style
FireFox中設置HTML標籤的style時,所有位置性和字體尺寸的值必須後跟px。這個ie也是支持的。
20. table操作
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。
IE:filter:alpha(opacity=10);
firefox:-moz-opacity:.10;
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都可以在兩種瀏覽器上使用。
27. XMLHTTP的區別

//mf 
if (window.XMLHttpRequest) //mf 
{ 
xmlhttp=new XMLHttpRequest() 
xmlhttp. 
xmlhttp.open("GET",url,true) 
xmlhttp.send(null) 
} 
//ie 
else if (window.ActiveXObject) // code for IE 
{ 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") 
    if (xmlhttp) 
    { 
    xmlhttp. 
    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); 


29. img的src刷新問題
在IE下可以用<img id="pic" οnclick= "this.src= &apos;aa.php&apos;" src="aa.php" style="cursor: pointer"/> 可以刷新圖片,但在FireFox下不行。主要是緩存問題,在地址後面加個隨機數就解決了。編輯onclick事件代碼如下: "this.src=this.src+'?'+Math.random()"。
30.!important
FF對於"!important"會自動優先解析,IE7已支持,然而IE6則會忽略。

.tabd{
background:url(/res/images/up/tab1.gif) no-repeat 0px 0px !important; /*Style for FF and IE7*/
background:url(/res/images/up/tab1.gif) no-repeat 1px 0px; /* Style for IE6 */
}


31.針對firefox ie6 ie7的css樣式
*+html 與 *html 是IE特有的標籤, firefox 暫不支持.而*+html 又爲 IE7特有標籤。

#color1 { color: #333; } /* FireFox */ 
* html #color1 { color: #666; } /* IE6 */ 
*+html #color1 { color: #999; } /* IE7 */ 


那麼在firefox下字體顏色顯示爲#333,IE6下字體顏色顯示爲#666,IE7下字體顏色顯示爲#999。注意它們的書寫順序。
*+html 對IE7的HACK 必須保證HTML頂部有如下聲明:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
32.margin加倍的問題
設置爲float的div在ie下設置的margin會加倍。這是一個ie6都存在的bug。
解決方案是在這個div裏面加上display:inline;

<style type="text/css">
#IamFloat{
  float:left;
  margin:5px;/*IE下理解爲10px*/
  display:inline;/*IE下再理解爲5px*/
}
</style>
<div id="imfloat"></div>


33.在mozilla firefox和IE中的BOX模型解釋不一致導致相差2px解決方法

div{
  margin:30px!important;
  margin:28px;
}


!important這個屬性IE不能識別,但別的瀏覽器可以識別。所以在IE下其實解釋成這樣:div{maring:30px;margin:28px},重複定義的話按照最後一個來執行。
34.禁止選取網頁內容
在IE中一般用js:obj.onselectstart=function(){return false;}
而firefox用CSS:-moz-user-select:none
35.捕獲事件
IE:obj.setCapture() 、obj.releaseCapture()
Firefox:
document.addEventListener("mousemove",mousemovefunction,true);
document.removeEventListener("mousemove",mousemovefunction,true);
36.document.documentElement
在HTML4.0標準下用document.body,在XHTML標準下就要換成document.documentElement。XHTML下document.body僅僅表示body對象,而不能代表文檔內容所渲染的整個表面。HTML下document.body.clientHeight表示瀏覽器的可視高度,XHTML下則是document.documentElement.clientHeight。document.body.clientHeight在XHTML下僅表示body的可視高度。在HTML4.0下用document.body.scrollTop;而在XHTML下則是document.documentElement.scrollTop,之前的document.body.scrollTop是恆爲0的。

//可以這樣兼容:
function getBodyObj(){
   return (document.documentElement) ? document.documentElement : document.body ;
}


37.min-height最小高度的實現(兼容IE6、IE7、FF)
最小高度min-height是一個非常有用的屬性,當容器的內容較少時,能保持一個最小的高度,以免破壞了佈局或UI設計效果。而當容器內的內容增加的時候,容器能夠自動的伸展以適應內容的變化。
min-height屬性並非所有瀏覽器都兼容,主要問題還是出現在IE6。
IE6對於overflow的特殊實現,給我們實現min-height提供了一個思路,所以產生了以下兼容IE6、IE7、FF瀏覽器的min-height寫法:

#main-container {
    background:#ccc;
    min-height:100px; 
    height:auto !important; 
    height:100px; 
    overflow:visible;
}


38.float閉合(clear:both)
如果上面用float控制了n列DIV,下面ie會自動檢測自動排列,ff則可能很不老實,到處亂動。
解決:
float結束後的下一個標籤加clear:both;以結束float的控制。
將以下代碼加入Global CSS 中,給需要閉合的div加上 class="clearfix" 即可!

/* Clear Fix */ 
.clearfix:after{ 
   content:"."; 
   display:block; 
   height:0; 
   clear:both; 
   visibility:hidden; 
} 
.clearfix{ 
   display:inline-block; 
} 
/* Hide from IE Mac */ 
.clearfix {display:block;} 
/* End hide from IE Mac */ 
/* end of clearfix */ 


:after(僞對象)--設置在對象後發生的內容,通常和content配合使用,IE不支持此僞對象,非Ie 瀏覽器支持。
39.最大/小寬度問題
min-width,max-width只是ff的命令,如何讓ie實現同樣的效果?
解決:
ie不認識min-和max-,實際ie認爲min-width、max-width和width效果一樣,可以用下面方法解決:

#cctext{
   min-width: 700px; 
   max-width: 1000px;
   width:expression(document.body.clientWidth<700 ? "700px" : document.body.clientWidth>1000 ? "1000px" : "auto");//只有ie認得 
}


40.塊元素居中對齊

body { text-align: center }/*for IE6*/
#content { text-align: left; width: 700px; margin: 0 auto }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章