javascript相容性匯總(IE/FF)

來源:互聯網
上載者:User

 .以下以 IE 代替 Internet Explorer,以 MF/FF 代替 Mozzila Firefox  

(1)window.event   

    IE:有window.event對象   

    FF:沒有window.event對象。可以通過給函數的參數傳遞event對象。如onmousemove=doMouseMove(event)   

(2)滑鼠當前座標   

    IE:event.x和event.y。   

    FF:event.pageX和event.pageY。   

    通用:兩者都有event.clientX和event.clientY屬性。   

(3)滑鼠當前座標(加上捲軸滾過的距離)   

     IE:event.offsetX和event.offsetY。   

     FF:event.layerX和event.layerY。   

(4)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的相容性問題。  

(5)event.toElement問題   

         問題: IE下,even對象有srcElement屬性,但是沒有target屬性;Firefox下,even對象有target屬性,但是沒有srcElement屬性   

         解決方案:   

               var target = e.relatedTarget || e.toElement;   

(6)標籤的x和y的座標位置:style.posLeft 和 style.posTop   

    IE:有。   

    FF:沒有。   

    通用:object.offsetLeft 和 object.offsetTop。   

(7)表單的高度和寬度   

    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。   

(8)添加事件   

     IE:element.attachEvent("onclick", function);。

           element.detachEvent("onclick",function);

           事件的執行順序: 棧(後進先出)

     FF:element.addEventListener("click", functiontrue)。

           element.removeEventListener('click',function,true)。

           事件的執行順序:隊列(先進先出) 

     通 用:element.onclick=function。雖然都可以使用onclick事件,但是onclick和上面兩種方法的效果是不一樣的,onclick 只有執行一個過程,而attachEvent和addEventListener執行的是一個過程列表,也就是多個過程。例如:element.attachEvent("onclick", func1);element.attachEvent("onclick", func2)這樣func1和func2都會被執行。   

(9)標籤的自訂屬性   

     IE:如果給標籤div1定義了一個屬性value,可以div1.value和div1["value"]取得該值。   

     FF:不能用div1.value和div1["value"]取。   

     通用:div1.getAttribute("value")。   

(10)document.form.item 問題  

     IE:現有問題:現有代碼中存在許多 document.formName.item("itemName") 這樣的語句,不能在 MF 下運行  

     FF/IE: document.formName.elements["elementName"]  

(11)集合/數組類對象問題  

     。現有問題:  

      現有代碼中許多集合類對象取用時使用 (),IE 能接受,MF 不能。  

     。解決方案:  

    改用 [] 作為下標運算。如:document.forms("formName") 改為 document.forms["formName"]。  

    又如:document.getElementsByName("inputName")(1) 改為 document.getElementsByName("inputName")[1]  

(12)HTML 對象的 id 作為對象名的問題  

    。現有問題  

       在 IE 中,HTML 對象的 ID 可以作為 document 的下屬物件變數名直接使用。在 MF 中不能。  

    。解決方案  

       用 getElementById("idName") 代替 idName 作為物件變數使用  

(13)用idName字串取得對象的問題  

    。現有問題  

       在IE中,利用 eval(idName) 可以取得 id 為 idName 的 HTML 對象,在MF 中不能。  

    。解決方案  

       用 getElementById(idName) 代替 eval(idName)。  

(14)變數名與某 HTML 對象 id 相同的問題  

    。現有問題  

       在 MF 中,因為對象 id 不作為 HTML 對象的名稱,所以可以使用與 HTML 對象 id 相同的變數名,IE 中不能。  

    。解決方案  

       在聲明變數時,一律加上 var ,以避免歧義,這樣在 IE 中亦可正常運行。  

       此外,最好不要取與 HTML 對象 id 相同的變數名,以減少錯誤。  

(15)document.getElementsByName() 和 document.all[name] 的問題  

        現有問題:在 IE 中,getElementsByName()、document.all[name] 均不能用來取得 div 元素(是否還有其它不能取的元素還不知道)。  

(16)document.all  

         Firefox可以相容document.all, 但會產生一條警告。可以用getElementById("*") 或者 getElementByTagName("*")來代替  

         不過對於document.all.length等屬性,則完全不相容  

(17)input.type屬性問題   

        說明:IE下input.type屬性為唯讀;但是Firefox下input.type屬性為讀寫  

(18)window.location.href問題   

         說明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location  

         解決方案:使用window.location來代替window.location.href  

(19)模態和非模態視窗問題   

         說明:IE下,可以通過showModalDialog和showModelessDialog開啟模態和非模態視窗;Firefox下則不能  

          解決方案:直接使用window.open(pageURL,name,parameters)方式開啟新視窗。   

          如果需要將子視窗中的參數傳遞迴父視窗,可以在子視窗中使用window.opener來訪問父視窗. 例如:var parWin = window.opener;                           parWin.document.getElementById("Aqing").value = "Aqing";   

(20)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";   

(21)body問題   

      Firefox的body在body標籤沒有被瀏覽器完全讀入之前就存在;而IE的body則必須在body標籤被瀏覽器完全讀入之後才存在  

(21)事件委託方法   

       IE:document.body.onload = inject; //Function inject()在這之前已被實現   

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

(22)firefox與IE的父元素(parentElement)的區別   

       IE:obj.parentElement   

       FF:obj.parentNode   

       解決方案: 因為FF與IE都支援DOM,因此使用obj.parentNode是不錯選擇  

(23)innerText在IE中能正常工作,但是innerText在FireFox中卻不行. 需用textContent  

(24)FireFox中設定HTML標籤的style時,所有位置性和字型尺寸的值必須後跟px。這個ie也是支援的  

(25)父節點、子節點和刪除節點   

      IE:parentElement、parement.children,element.romoveNode(true)。   

      FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。   

(26)對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;  

(27)捕獲事件   

      問題: 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);   }   

  

(28)禁止選取網頁內容   

         問題:FF需要用CSS禁止,IE用JS禁止   

         解決方案:   

              IE: obj.onselectstart = function() {return false;}   

              FF: -moz-user-select:none;   

(29)畫圖   

         IE:VML。   

         FF:SVG。   

(30)CSS:透明   

      IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。   

      FF:opacity:0.6。   

(31)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;。   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.