[轉載]JS瀏覽器兼容性問題

from:http://dada-fangfang.iteye.com/blog/811749javascript

作BS開發就不免會用到javascript,而每一個瀏覽器對javascript的支持有不一樣。這就須要咱們程序員去兼容他們,否則有些瀏覽器就沒法運行咱們的代碼。就會造來客戶的投訴,若是讓BoSS知道了,這可不太好哦。下面是兼容IE和FF的js腳本作法和分解(部分選自網上):
 
 
.如下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefox
 
//window.event  
IE:有window.event對象  
FF:沒有window.event對象。能夠經過給函數的參數傳遞event對象。如onmousemove=doMouseMove(event)  
 
//鼠標當前座標  
IE:event.x和event.y。  
FF:event.pageX和event.pageY。  
通用:二者都有event.clientX和event.clientY屬性。  
 
//鼠標當前座標(加上滾動條滾過的距離)  
IE:event.offsetX和event.offsetY。  
FF:event.layerX和event.layerY。  
 
//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的兼容性問題。
 
//event.toElement問題  
問題:  
IE下,even對象有srcElement屬性,可是沒有target屬性;Firefox下,even對象有target屬性,可是沒有srcElement屬性  
解決方法:  
var target = e.relatedTarget || e.toElement;  
 
//標籤的x和y的座標位置:style.posLeft 和 style.posTop  
IE:有。  
FF:沒有。  
通用:object.offsetLeft 和 object.offsetTop。  
 
//窗體的高度和寬度  
IE:document.body.offsetWidth和document.body.offsetHeight。注意:此時頁面必定要有body標籤。  
FF:window.innerWidth和window.innerHegiht,以及document.documentElement.clientWidth和document.documentElement.clientHeight。  
通用:document.body.clientWidth和document.body.clientHeight。  
 
//添加事件  
IE:element.attachEvent("onclick", function);。  
FF:element.addEventListener("click", function, true)。  
通 用:element.onclick=function。雖然均可以使用onclick事件,可是onclick和上面兩種方法的效果是不同的,onclick 只有執行一個過程,而attachEvent和addEventListener執行的是一個過程列表,也就是多個過程。例如:element.attachEvent("onclick", func1);element.attachEvent("onclick", func2)這樣func1和func2都會被執行。  
 
//標籤的自定義屬性  
IE:若是給標籤div1定義了一個屬性value,能夠div1.value和div1["value"]取得該值。  
FF:不能用div1.value和div1["value"]取。  
通用:div1.getAttribute("value")。  
 
//document.form.item 問題
IE:現有問題:現有代碼中存在許多 document.formName.item("itemName") 這樣的語句,不能在 MF 下運行
FF/IE: document.formName.elements["elementName"]
 
//集合/數組類對象問題
(1)現有問題:
    現有代碼中許多集合類對象取用時使用 (),IE 能接受,MF 不能。
(2)解決方法:
    改用 [] 做爲下標運算。如:document.forms("formName") 改成 document.forms["formName"]。
    又如:document.getElementsByName("inputName")(1) 改成 document.getElementsByName("inputName")[1]
 
//HTML 對象的 id 做爲對象名的問題
(1)現有問題
     在 IE 中,HTML 對象的 ID 能夠做爲 document 的下屬對象變量名直接使用。在 MF 中不能。
(2)解決方法
     用 getElementById("idName") 代替 idName 做爲對象變量使用
 
//用idName字符串取得對象的問題
(1)現有問題
     在IE中,利用 eval(idName) 能夠取得 id 爲 idName 的 HTML 對象,在MF 中不能。
(2)解決方法
     用 getElementById(idName) 代替 eval(idName)。
 
//變量名與某 HTML 對象 id 相同的問題
(1)現有問題
    在 MF 中,由於對象 id 不做爲 HTML 對象的名稱,因此能夠使用與 HTML 對象 id 相同的變量名,IE 中不能。
(2)解決方法
    在聲明變量時,一概加上 var ,以免歧義,這樣在 IE 中亦可正常運行。
    此外,最好不要取與 HTML 對象 id 相同的變量名,以減小錯誤。
 
//document.getElementsByName() 和 document.all[name] 的問題
現有問題:在 IE 中,getElementsByName()、document.all[name] 均不能用來取得 div 元素(是否還有其它不能取的元素還不知道)。
//document.all
Firefox能夠兼容document.all, 但會生成一條警告。能夠用getElementById("*") 或者 getElementByTagName("*")來代替
不過對於document.all.length等屬性,則徹底不兼容
 
//input.type屬性問題  
說明:IE下input.type屬性爲只讀;可是Firefox下input.type屬性爲讀寫
 
//window.location.href問題  
說明:IE或者Firefox2.0.x下,能夠使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location
解決方法:使用window.location來代替window.location.href
 
//模態和非模態窗口問題  
說明:IE下,能夠經過showModalDialog和showModelessDialog打開模態和非模態窗口;Firefox下則不能
解決方法:直接使用window.open(pageURL,name,parameters)方式打開新窗口。  
若是須要將子窗口中的參數傳遞迴父窗口,能夠在子窗口中使用window.opener來訪問父窗口. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";  
 
//frame問題  
如下面的frame爲例:  
<frame src="xxx.html" mce_src="xxx.html" id="frameId" name="frameName" />  
(1)訪問frame對象:  
IE:使用window.frameId或者window.frameName來訪問這個frame對象. frameId和frameName能夠同名。  
FF:只能使用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來訪問父窗口。例如:window.parent.document.form1.filename.value="Aqing";  
 
//body問題  
Firefox的body在body標籤沒有被瀏覽器徹底讀入以前就存在;而IE的body則必須在body標籤被瀏覽器徹底讀入以後才存在
 
//事件委託方法  
IE:document.body.onload = inject; //Function inject()在這以前已被實現  
FF:document.body.onload = inject();  
 
//firefox與IE的父元素(parentElement)的區別  
IE:obj.parentElement  
FF:obj.parentNode  
解決方法: 由於FF與IE都支持DOM,所以使用obj.parentNode是不錯選擇
 
//innerText在IE中能正常工做,可是innerText在FireFox中卻不行. 需用textContent
 
//FireFox中設置HTML標籤的style時,全部位置性和字體尺寸的值必須後跟px。這個ie也是支持的
 
//父節點、子節點和刪除節點  
IE:parentElement、parement.children,element.romoveNode(true)。  
FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。  
 
//對select的options集合操做  
枚舉元素除了[]外,SelectName.options.item()也是能夠的, 另外SelectName.options.length, SelectName.options.add/remove均可以在兩種瀏覽器上使用。
注意在add後賦值元素,不然會失敗
動態刪除select中的全部options:  
       document.getElementById("ddlResourceType").options.length=0;  
動態刪除select中的某一項option:  
       document.getElementById("ddlResourceType").options.remove(indx);   
動態添加select中的項option:  
       document.getElementById("ddlResourceType").options.add(new Option(text,value));  
IE FF 動態刪除通用方法:
document.getElementById("ddlResourceType").options[indx] = null;
 
//捕獲事件  
問題:  
FF沒有setCapture()、releaseCapture()方法  
解決方法:  
IE:  
obj.setCapture();  
obj.releaseCapture();  
FF:  
window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);  
window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);  
if (!window.captureEvents) {  
       o.setCapture();  
}else {  
       window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);  
}  
if (!window.captureEvents) {  
       o.releaseCapture();  
}else {  
       window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);  
}  
 
 
//禁止選取網頁內容  
問題:  
FF須要用CSS禁止,IE用JS禁止  
解決方法:  
IE: obj.onselectstart = function() {return false;}  
FF: -moz-user-select:none;  
 
 
//畫圖  
IE:VML。  
FF:SVG。  
 
//CSS:透明  
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。  
FF:opacity:0.6。  
 
//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;。  
 
//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;。html

相關文章
相關標籤/搜索