瀏覽器兼容之JavaScript篇——已在IE、FF、Chrome測試

在瀏覽器兼容性問題解決方法,已在IE、FF、Chrome測試中,已經對關於CSS的一些兼容性問題進行了總結,後來自己又整理了一些關於JS的兼容性問題,現在分享給大家。

1)ChildNodes

問題:

IE7、IE8正常,IE9、IE10、chrome、FF的 childNodes 中會插入空白文本節點

解決:

可以通過document.getElementsByName 來回避這個問題

實例:

[html] view plain copy

<span style="font-size:18px;"><script type="text/javascript" >  
    function IETest(){  
        var testdiv=document.getElementById("div1");  
        var ieLength=testdiv.childNodes.length;  
        alert(ieLength);  
        }  
    function FFTest(){  
        var testdiv=document.getElementById("div1");  
        var ffLength=testdiv.childNodes.length;  
        alert(ffLength);  
        }  
    function Solution(){  
        var solution =new Array();  
        var solution=document.getElementsByName("test");  
        alert(solution.length);  
        }  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>  
     <div id="div1">  
    <a name="test"></a>  
        <a name="test"></a>  
        <a name="test"></a>  
    </div>  
    <input type="button"  value="FFTest" onclick="FFTest()"/>  
    <input type="button" value="IETest" onclick="IETest()"/>  
    <input type="button" value="Solution" onclick="Solution()" />  
</body></span>  

2)Const問題

問題:

在IE中不能識別const,而FF和chrome的可以

解決:

使用Var,不使用const

實例:

[html] view plain copy

<span style="font-size:18px;"><script type="text/javascript">  
function OK(){  
   function right()  
   {  
   var n="我是var定義的值";  
   alert(n);  
   }  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>  
<div style="cursor:pointer;" onClick="right();">點我啊!</div>  
</body></span>  

3)innerText問題

問題:

    innerText 在IE和谷歌中能正常工作,但是 innerText 在FireFox中卻不行
 textContent在IE9、IE10、谷歌、FF中能正常工作,但是IE7、IE8中不能使用

解決:

不同的瀏覽器採用不同方式。

實例:

[html] view plain copy

<span style="font-size:18px;"><script type="text/javascript">  
function isIE()  
{  
   var i=navigator.userAgent.toLowerCase().indexOf("msie");  
   if(i>0)  { return true;}  
   else    { return false;}  
}  
function isFireFox(){  
    var j=navigator.userAgent.toLowerCase().indexOf("firefox");  
    if(j>0)  { return true;}  
    else    { return false;}  
}  
 function show(){  
  var obj = document.getElementById("d");  
  //d.innerText="show text";//火狐不識別  
    if(isIE()){  
    d.innerText="show 新年快快樂樂!";  
    }  
    else if(isFireFox()){  
  d.textContent="新年快樂!";//IE7,8不識別其他都行   
  }  
else   
{  
d.textContent="新年快樂!";  
}  
 }  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<p id="d" onClick="show();">點我啊!</p>   
</body></span>  

4)Input的type屬性

問題:

在IE10、chrome和FF中type屬性可以修改,但是在IE7、IE8、IE9中不能修改,因爲在這裏input屬性type是隻讀的。

解決:

利用jquery和隱藏控件,實現對type屬性的修改

實例:

[html] view plain copy

<span style="font-size:18px;"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>  
    <script type="text/javascript">  
        function showPwd(chk) {  
            var $2 = $("#tbPwd,#tbPwd_Text");  
            var currVal = $2.filter(":visible").val();  
            $2.toggle().val(currVal);  
        }  
    </script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>  
    明文顯示:<input type="checkbox" id="CBShowPwd" onchange="showPwd(this)" /><br />  
    密碼:<input type="password" id="tbPwd"  />  
    <input type="text" id="tbPwd_Text" style="display:none;background-color:coral;" />  
</body></span>  

5)window.event事件

問題:

    在FF中無法使用,IE系列和Chrome正常

解決:

可以通過給函數的參數傳遞event對象,然後獲取後進行處理

實例:

[html] view plain copy

<span style="font-size:18px;"><style type="text/css">  
 #obj{background:red;width:300px;height:200px;position:relative;}     
 </style>  
<script type="text/javascript">  
function show(eent) {  
  var event = eent || window.event;  
    var x = event.x || event.pageX;  
    var y = event.y || event.pageY;  
  alert("x座標爲"+x+",y座標爲"+y);  
  }  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<table border=1 cellpadding=15 cellspacing=15 style="position:relative;left:100;top:100">  
<tr><td>  
<div onClick="show(event)" style="background:silver;cursor:pointer">  
Click here to show.   
</div>  
</td></tr>  
</table>  
</body></span>  

6)變量名與某HTML對象 id 相同的問題

問題:

在FF、Chrome和IE9、IE10中,因爲對象 id 不作爲HTML對象的名稱,所以可以使用與HTML對象 id 相同的變量名,但是在IE7、IE8中不能

解決:

       在聲明變量時,一律加上 var ,以避免歧義,這樣在IE7、IE8中亦可正常運行,最好不要取與HTML對象 id 有相同的變量名,以減少錯誤。

實例:

[html] view plain copy

<span style="font-size:18px;"><style type="text/css">  
 #obj{background:red;width:300px;height:200px;position:relative;}     
 </style>  
<script type="text/javascript">  
function IeTest() {  
  Test="124";  //IE78不能使用  
 alert(Test);  
}  
function Solution() {  
  var IETest="124";  
 alert(IETest);  
}  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<input name="測試" value="測試" id="Test" onClick="IeTest();" type="button">  
<input name="解決方案" value="解決方案" id="Solution" onClick="Solution();" type="button">  
</body></span>  

7)對象寬高賦值問題

問題:

FF、IE、chrome中類似 obj.style.height = imgObj.height 的語句無效

解決:

     統一使用:obj.style.height = imgObj. offsetHeight+ "px"

實例:

[html] view plain copy

<span style="font-size:18px;"><script type="text/javascript">   
         function $(id){    
           return document.getElementById(id);   
        }    
        function getHeight() {    
          alert($("hidden").offsetHeight+ "px" );    
          $("pinglun").style.height=$("hidden").offsetHeight+ "px";              
        }    
      window.onload = function() {    
          getHeight();    
         }    
   </script>   
 <style type="text/css">   
   #hidden { width:100px; background:#99CC00; height:400px; float:left}    
   #pinglun { width:500px; height:auto; background:#FFCCCC; float:left; margin-left:5px;}    
</style> </span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<div id="hidden">這是要設置的高度!</div>   
  <div id="pinglun" >高度載入……</div>   
</body> </span>  

8)集合類對象問題

問題:

代碼中許多集合類對象取用時使用(),IE能接受,FF和chrome不能接受

解決:

     改用 [] 作爲下標運算,例:
    document.getElementsByName("idName")(0)

    改爲

    document.getElementsByName("idName")[0]

實例:

[html] view plain copy

<span style="font-size:18px;"><script type="text/javascript">  
function ok(){  
 var s= document.getElementsByName("idName")[0].value;  
 alert(s);  
}  
 window.onload=ok;  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<input name="idName" type="text" value="HEllO">  
</body></span>  

9)禁止選取網頁內容

問題:

      禁止選取網頁內容,FF需要用CSS禁止,而IE系列和谷歌用JS禁止

解決:

IE: document.onselectstart=function() {return false;}
FF:-moz-user-select:none;

實例:

[html] view plain copy

<span style="font-size:18px;"><style type=text/css>  
#m_Div{  
width:200px;  
height:200px;  
border:1px solid #999;  
padding:2px;  
-moz-user-select:none;  
}  
</style>  
<script type="text/javascript">  
document.onselectstart=function() {return false;}  
</script></span>  

[html] view plain copy

<span style="font-size:18px;"><body>   
<div id="m_Div";">點一下IE的菜單或者按鈕看看:)  
又或者IE窗口外的地方</div>  
</body></span>  

總結:

BS的項目開發,難免會用到JS,而每個瀏覽器對JS的支持不同。這就需要我們去兼容他們,不然有些瀏覽器就無法運行我們的代碼,實現相應的功能。對此,我們要注意各個瀏覽器之間的差異,相同的JS,在項目中解析不同,識別這些不同,進而避免,找到解決方法,這是我們需要做的事情。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章