浏览器兼容之JavaScript篇——已在IE、FF、Chrome测试

来源:互联网 发布:电脑flash制作软件 编辑:程序博客网 时间:2024/05/22 00:12

  浏览器兼容性问题解决方法,已在IE、FF、Chrome测试中,已经对关于CSS的一些兼容性问题进行了总结,后来自己又整理了一些关于JS的兼容性问题,现在分享给大家。

1)ChildNodes

问题:

    IE7、IE8正常,IE9、IE10、chrome、FF的 childNodes 中会插入空白文本节点

解决:

    可以通过document.getElementsByName 来回避这个问题

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script type="text/javascript" >  
  2.     function IETest(){  
  3.         var testdiv=document.getElementById("div1");  
  4.         var ieLength=testdiv.childNodes.length;  
  5.         alert(ieLength);  
  6.         }  
  7.     function FFTest(){  
  8.         var testdiv=document.getElementById("div1");  
  9.         var ffLength=testdiv.childNodes.length;  
  10.         alert(ffLength);  
  11.         }  
  12.     function Solution(){  
  13.         var solution =new Array();  
  14.         var solution=document.getElementsByName("test");  
  15.         alert(solution.length);  
  16.         }  
  17. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>  
  2.      <div id="div1">  
  3.     <a name="test"></a>  
  4.         <a name="test"></a>  
  5.         <a name="test"></a>  
  6.     </div>  
  7.     <input type="button"  value="FFTest" onclick="FFTest()"/>  
  8.     <input type="button" value="IETest" onclick="IETest()"/>  
  9.     <input type="button" value="Solution" onclick="Solution()" />  
  10. </body></span>  

2)Const问题

问题:

    在IE中不能识别const,而FF和chrome的可以

解决:

    使用Var,不使用const

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script type="text/javascript">  
  2. function OK(){  
  3.    function right()  
  4.    {  
  5.    var n="我是var定义的值";  
  6.    alert(n);  
  7.    }  
  8. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>  
  2. <div style="cursor:pointer;" onClick="right();">点我啊!</div>  
  3. </body></span>  

3)innerText问题

问题:

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

解决:

    不同的浏览器采用不同方式。

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script type="text/javascript">  
  2. function isIE()  
  3. {  
  4.    var i=navigator.userAgent.toLowerCase().indexOf("msie");  
  5.    if(i>0)  { return true;}  
  6.    else    { return false;}  
  7. }  
  8. function isFireFox(){  
  9.     var j=navigator.userAgent.toLowerCase().indexOf("firefox");  
  10.     if(j>0)  { return true;}  
  11.     else    { return false;}  
  12. }  
  13.  function show(){  
  14.   var obj = document.getElementById("d");  
  15.   //d.innerText="show text";//火狐不识别  
  16.     if(isIE()){  
  17.     d.innerText="show 新年快快乐乐!";  
  18.     }  
  19.     else if(isFireFox()){  
  20.   d.textContent="新年快乐!";//IE7,8不识别其他都行   
  21.   }  
  22. else   
  23. {  
  24. d.textContent="新年快乐!";  
  25. }  
  26.  }  
  27. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <p id="d" onClick="show();">点我啊!</p>   
  3. </body></span>  

4)Input的type属性

问题:

    在IE10、chrome和FF中type属性可以修改,但是在IE7、IE8、IE9中不能修改,因为在这里input属性type是只读的。

解决:

   利用jquery和隐藏控件,实现对type属性的修改

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>  
  2.     <script type="text/javascript">  
  3.         function showPwd(chk) {  
  4.             var $2 = $("#tbPwd,#tbPwd_Text");  
  5.             var currVal = $2.filter(":visible").val();  
  6.             $2.toggle().val(currVal);  
  7.         }  
  8.     </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>  
  2.     明文显示:<input type="checkbox" id="CBShowPwd" onchange="showPwd(this)" /><br />  
  3.     密码:<input type="password" id="tbPwd"  />  
  4.     <input type="text" id="tbPwd_Text" style="display:none;background-color:coral;" />  
  5. </body></span>  

5)window.event事件

问题:

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

解决:

    可以通过给函数的参数传递event对象,然后获取后进行处理

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><style type="text/css">  
  2.  #obj{background:red;width:300px;height:200px;position:relative;}     
  3.  </style>  
  4. <script type="text/javascript">  
  5. function show(eent) {  
  6.   var event = eent || window.event;  
  7.     var x = event.x || event.pageX;  
  8.     var y = event.y || event.pageY;  
  9.   alert("x坐标为"+x+",y坐标为"+y);  
  10.   }  
  11. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <table border=1 cellpadding=15 cellspacing=15 style="position:relative;left:100;top:100">  
  3. <tr><td>  
  4. <div onClick="show(event)" style="background:silver;cursor:pointer">  
  5. Click here to show.   
  6. </div>  
  7. </td></tr>  
  8. </table>  
  9. </body></span>  

6)变量名与某HTML对象 id 相同的问题

问题:

    在FF、Chrome和IE9、IE10中,因为对象 id 不作为HTML对象的名称,所以可以使用与HTML对象 id 相同的变量名,但是在IE7、IE8中不能

解决:

           在声明变量时,一律加上 var ,以避免歧义,这样在IE7、IE8中亦可正常运行,最好不要取与HTML对象 id 有相同的变量名,以减少错误。

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><style type="text/css">  
  2.  #obj{background:red;width:300px;height:200px;position:relative;}     
  3.  </style>  
  4. <script type="text/javascript">  
  5. function IeTest() {  
  6.   Test="124";  //IE78不能使用  
  7.  alert(Test);  
  8. }  
  9. function Solution() {  
  10.   var IETest="124";  
  11.  alert(IETest);  
  12. }  
  13. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <input name="测试" value="测试" id="Test" onClick="IeTest();" type="button">  
  3. <input name="解决方案" value="解决方案" id="Solution" onClick="Solution();" type="button">  
  4. </body></span>  

7)对象宽高赋值问题

问题:

    FF、IE、chrome中类似 obj.style.height = imgObj.height 的语句无效

解决:

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

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script type="text/javascript">   
  2.          function $(id){    
  3.            return document.getElementById(id);   
  4.         }    
  5.         function getHeight() {    
  6.           alert($("hidden").offsetHeight+ "px" );    
  7.           $("pinglun").style.height=$("hidden").offsetHeight+ "px";              
  8.         }    
  9.       window.onload = function() {    
  10.           getHeight();    
  11.          }    
  12.    </script>   
  13.  <style type="text/css">   
  14.    #hidden { width:100px; background:#99CC00; height:400px; float:left}    
  15.    #pinglun { width:500px; height:auto; background:#FFCCCC; float:left; margin-left:5px;}    
  16. </style> </span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <div id="hidden">这是要设置的高度!</div>   
  3.   <div id="pinglun" >高度载入……</div>   
  4. </body> </span>  

8)集合类对象问题

问题:

    代码中许多集合类对象取用时使用(),IE能接受,FF和chrome不能接受

解决:

         改用 [] 作为下标运算,例:
        document.getElementsByName("idName")(0)

        改为

        document.getElementsByName("idName")[0]

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><script type="text/javascript">  
  2. function ok(){  
  3.  var sdocument.getElementsByName("idName")[0].value;  
  4.  alert(s);  
  5. }  
  6.  window.onload=ok;  
  7. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <input name="idName" type="text" value="HEllO">  
  3. </body></span>  

9)禁止选取网页内容

问题:  

          禁止选取网页内容,FF需要用CSS禁止,而IE系列和谷歌用JS禁止

解决:  

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

    FF:-moz-user-select:none;

实例:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><style type=text/css>  
  2. #m_Div{  
  3. width:200px;  
  4. height:200px;  
  5. border:1px solid #999;  
  6. padding:2px;  
  7. -moz-user-select:none;  
  8. }  
  9. </style>  
  10. <script type="text/javascript">  
  11. document.onselectstart=function() {return false;}  
  12. </script></span>  
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><body>   
  2. <div id="m_Div";">点一下IE的菜单或者按钮看看:)  
  3. 又或者IE窗口外的地方</div>  
  4. </body></span>  


总结:

    BS的项目开发,难免会用到JS,而每个浏览器对JS的支持不同。这就需要我们去兼容他们,不然有些浏览器就无法运行我们的代码,实现相应的功能。对此,我们要注意各个浏览器之间的差异,相同的JS,在项目中解析不同,识别这些不同,进而避免,找到解决方法,这是我们需要做的事情。

0 0
原创粉丝点击