常用的相容IE和FirefoxFF等瀏覽器的js方法(js中ie和Firefox的一些差別)

來源:互聯網
上載者:User

標籤:des   style   使用   width   os   art   

介紹了網頁上常用的IE/Firefox相容性該頁的做法,並給出了代碼,相當實用了。為了方便大家閱讀代碼,以下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefox 。以下進入正題:

//window.event
IE:有window.event對象
FF:沒有window.event對象。可以通過給函數的參數傳遞event對象。如onmousemove=doMouseMove(event)
解決方案:var event = event || window.event;一個樣本:

<script>function test(event) {var event = event || window.event;//do Something}</script><input type="button" value="click" onclick="test(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。
解決方案還是有的,請看下面的代碼:

<script>function test(event) {var event = event || window.event;//or var event = event ? event : window.event;//這2中都可以,也可以用if else(這簡寫)var x = event.offsetX || event.layerX;var y = event.offsetY || event.layerY;//do Something}</script><div onmousedown="test(event)"></div>

//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 作為對象名的問題
在 IE 中,HTML 對象的 ID 可以作為 document 的下屬物件變數名直接使用。在 MF 中不能。
解決方案:用 getElementById("idName") 代替 idName 作為物件變數使用

//用idName字串取得對象的問題
在IE中,利用 eval(idName) 可以取得 id 為 idName 的 HTML 對象,在MF 中不能。
解決方案:用 getElementById(idName) 代替 eval(idName)。

//變數名與某 HTML 對象 id 相同的問題
在 MF 中,因為對象 id 不作為 HTML 對象的名稱,所以可以使用與 HTML 對象 id 相同的變數名,IE 中不能。
解決方案:在聲明變數時,一律加上 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";

//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();
FFFirefox中的解決辦法:
 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.