js兼容瀏覽器問題-兼容IE和FireFox的解決方法


做BS開發難免會用到JavaScript,而不同瀏覽器對JavaScript的支持有所不同,這就需要我們去思考如何寫出兼容不同瀏覽器的js代碼。(這方面jQuery就做的很好,jQuery的簡潔代碼通常能夠很好地兼容不同瀏覽器)。

以下以 IE 代替 Internet Explorer,以 FF 代替 Mozzila Firefox:

// window.event
說明:window.event對象代表事件的狀態,例如觸發event對象的元素、鼠標的位置及狀態、按下的鍵等等。 event對象只在事件發生的過程中才有效。event對象的某些屬性只對特定的事件有意義。比如,fromElement 和 toElement 屬性只對 onmouseover 和 onmouseout 事件有意義。
IE:有window.event對象
FF:沒有window.event對象。解決辦法:可以通過給函數參數傳遞event對象。如onmousemove=doMouseMove(event)
兼容代碼:var event = event || window.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
兼容代碼:
function mouseDownHandler(event) {
    var event = event || window.event;
    var x = event.offsetX || event.layerX;
    var y = event.offsetX || event.layerY;
}

// event.srcElement
說明:返回觸發事件的元素。只讀屬性。
IE:event對象有srcElement屬性,但是沒有target屬性
FF:even對象沒有srcElement屬性,但是有target屬性
兼容代碼:使用 var obj = event.srcElement ? event.srcElement : event.target; 來代替IE下的event.srcElement或者FF下的event.target。

// event.toElement
說明:檢測 onmouseover 和 onmouseout 事件發生時,鼠標所進入的元素。只讀屬性。
IE:event對象有toElement屬性,但沒有relatedTarget屬性
FF:event對象沒有toElement屬性,但有relatedTarget屬性
兼容代碼:var target = event.toElement || event.relatedTarget;

// 標簽的x和y的坐標位置 style.posLeft 和 style.posTop
IE:有。
FF:沒有。
兼容代碼:使用通用屬性obj.offsetLeft 和 obj.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:
FF:現有代碼中存在許多 document.formName.item("itemName") 這樣的語句,不能在 FF 下運行
兼容代碼:document.formName.elements["elementName"]

// 集合/數組對象問題
IE:有許多集合類對象取用時用()
FF:不能這樣取用

兼容代碼:改用 [] 作為下標運算。如: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;。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM