IE和Firefox(火狐)在JavaScript方面的不兼容及统一方法总结

  1. 测试代码时,发现不少IE可以运行的ajax,但在FF中报错。   
  2. IE和Firefox(火狐)在JavaScript方面的不兼容及统一方法总结如下:   
  3. 1.兼容firefox的 outerHTML,FF中没有outerHtml的方法。   
  4.   
  5. if (window.HTMLElement) {   
  6.   HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML) {   
  7.         var r=this.ownerDocument.createRange();   
  8.         r.setStartBefore(this);   
  9.         var df=r.createContextualFragment(sHTML);   
  10.         this.parentNode.replaceChild(df,this);   
  11.         return sHTML;   
  12.     });   
  13.   
  14.     HTMLElement.prototype.__defineGetter__("outerHTML",function() {   
  15.      var attr;   
  16.         var attrs=this.attributes;   
  17.         var str="<"+this.tagName.toLowerCase();   
  18.         for (var i=0;i<attrs.length;i++) {   
  19.             attr=attrs[i];   
  20.             if(attr.specified)   
  21.                 str+=" "+attr.name+'="'+attr.value+'"';   
  22.         }   
  23.         if(!this.canHaveChildren)   
  24.             return str+">";   
  25.         return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";   
  26.         });   
  27.   
  28.    HTMLElement.prototype.__defineGetter__("canHaveChildren",function() {   
  29.      switch(this.tagName.toLowerCase()) {   
  30.          case "area":   
  31.          case "base":   
  32.          case "basefont":   
  33.          case "col":   
  34.          case "frame":   
  35.          case "hr":   
  36.          case "img":   
  37.          case "br":   
  38.          case "input":   
  39.          case "isindex":   
  40.          case "link":   
  41.          case "meta":   
  42.          case "param":   
  43.          return false;   
  44.      }   
  45.      return true;   
  46.    });   
  47. }   
  48.   
  49.   
  50. 2.集合类对象问题   
  51.   
  52. 说明:IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象.   
  53. 解决方法:统一使用[]获取集合类对象.   
  54.   
  55. 3.自定义属性问题   
  56.   
  57. 说明:IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性.   
  58. 解决方法:统一通过getAttribute()获取自定义属性.   
  59.   
  60.   
  61. 4.eval("idName")问题   
  62.   
  63. 说明:IE下,,可以使用eval("idName")或getElementById("idName")来取得id为idName的HTML对象;Firefox下只能使用getElementById("idName")来取得id为idName的HTML对象.   
  64. 解决方法:统一用getElementById("idName")来取得id为idName的HTML对象.    
  65.   
  66. 5.变量名与某HTML对象ID相同的问题   
  67.   
  68. 说明:IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox下,可以使用与HTML对象ID相同的变量名;IE下则不能。   
  69. 解决方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义.   
  70.   
  71. 6.const问题   
  72.   
  73. 说明:Firefox下,可以使用const关键字或var关键字来定义常量;IE下,只能使用var关键字来定义常量.   
  74. 解决方法:统一使用var关键字来定义常量.   
  75.   
  76. 7.input.type属性问题   
  77.   
  78. 说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写.   
  79.   
  80. 8.window.event问题   
  81.   
  82. 说明:window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用.   
  83. 解决方法:   
  84. IE:   
  85. <input name="Button8_1" type="button" value="IE" οnclick="javascript:gotoSubmit8_1()"/>   
  86. ...   
  87. <script language="javascript">   
  88. function gotoSubmit8_1() {   
  89. ...   
  90. alert(window.event); //use window.event   
  91. ...   
  92. }   
  93. </script>   
  94. IE&Firefox:   
  95. <input name="Button8_2" type="button" value="IE" οnclick="javascript:gotoSubmit8_2(event)"/>   
  96. ...   
  97. <script language="javascript">   
  98. function gotoSubmit8_2(evt) {   
  99. ...   
  100. evt=evt?evt:(window.event?window.event:null);   
  101. alert(evt); //use evt   
  102. ...   
  103. }   
  104. </script>   
  105.   
  106. 9.event.x与event.y问题   
  107.   
  108. 说明:IE下,even对象有x,y属性,但是没有pageX,pageY属性;Firefox下,even对象有pageX,pageY属性,但是没有x,y属性.   
  109. 解决方法:使用mX(mX = event.x ? event.x : event.pageX;)来代替IE下的event.x或者Firefox下的event.pageX.   
  110.   
  111.   
  112. 10.event.srcElement问题   
  113.   
  114. 说明:IE下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性.   
  115. 解决方法:使用obj(obj = event.srcElement ? event.srcElement : event.target;)来代替IE下的event.srcElement或者Firefox下的event.target.    
  116.   
  117. 11.window.location.href问题   
  118.   
  119. 说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location.   
  120. 解决方法:使用window.location来代替window.location.href.   
  121.   
  122. 12.模态和非模态窗口问题   
  123.   
  124. 说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能.   
  125. 解决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。   
  126.   
  127. 如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";   
  128.   
  129. 13.frame问题   
  130.   
  131. 以下面的frame为例:   
  132. <frame src="xxx.html" id="frameId" name="frameName" />   
  133.   
  134. (1)访问frame对象:   
  135. IE:使用window.frameId或者window.frameName来访问这个frame对象.   
  136. Firefox:只能使用window.frameName来访问这个frame对象.   
  137. 另外,在IE和Firefox中都可以使用window.document.getElementById("frameId")来访问这个frame对象.   
  138.   
  139. (2)切换frame内容:   
  140. 在IE和Firefox中都可以使用window.document.getElementById("testFrame").src = "xxx.html"或window.frameName.location = "xxx.html"来切换frame的内容.   
  141.   
  142. 如果需要将frame中的参数传回父窗口,可以在frme中使用parent来访问父窗口。例如:parent.document.form1.filename.value="Aqing";   
  143.   
  144. 14.body问题   
  145.   
  146. Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.   
  147.   
  148. 例如:   
  149. Firefox:   
  150. <body >   
  151. <script type="text/javascript">   
  152. document.body.onclick = function(evt){   
  153. evt = evt &#124;&#124; window.event;   
  154. alert(evt);   
  155. }   
  156. </script>   
  157. </body>   
  158. IE&Firefox:   
  159. <body >   
  160. </body>   
  161. <script type="text/javascript">   
  162. document.body.onclick = function(evt){   
  163. evt = evt &#124;&#124; window.event;   
  164. alert(evt);   
  165. } </script>   
  166.   
  167.   
  168. 15. 事件委托方法   
  169.   
  170. IE:document.body.onload = inject; //Function inject()在这之前已被实现   
  171.   
  172. Firefox:document.body.onload = inject();   
  173.   
  174. 有人说标准是:   
  175.   
  176. document.body.οnlοad=new Function('inject()');   
  177.   
  178.   
  179. 16. firefox与IE(parentElement)的父元素的区别   
  180.   
  181. IE:obj.parentElement   
  182. firefox:obj.parentNode   
  183.   
  184. 解决方法: 因为firefox与IE都支持DOM,因此使用obj.parentNode是不错选择.   
  185.   
  186.   
  187. 17.cursor:hand VS cursor:pointer   
  188.   
  189. firefox不支持hand,但ie支持pointer   
  190.   
  191. 解决方法: 统一使用pointer   
  192.   
  193.   
  194. 18.innerText在IE中能正常工作,但是innerText在FireFox中却不行.   
  195.   
  196. 解决方法:   
  197.   
  198. if(navigator.appName.indexOf("Explorer") > -1){   
  199.   
  200.     document.getElementById('element').innerText = "my text";   
  201.   
  202. else{   
  203.   
  204.     document.getElementById('element').textContent = "my text";   
  205.   
  206. }   
  207.   
  208.   
  209. 19. FireFox中类似 obj.style.height = imgObj.height 的语句无效   
  210.   
  211. 解决方法:   
  212.   
  213. obj.style.height = imgObj.height + 'px';   
  214.   
  215.   
  216. 20. IE,firefox以及其它浏览器对于 table 标签的操作都各不相同,在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChile方法也不管用。   
  217.   
  218. 解决方法:   
  219.   
  220. //向table追加一个空行:   
  221. var row = otable.insertRow(-1);   
  222. var cell = document.createElement("td");   
  223. cell.innerHTML = " ";   
  224. cell.className = "XXXX";   
  225. row.appendChild(cell);   
  226.   
  227.   
  228. 21. padding 问题   
  229.   
  230. padding 5px 4px 3px 1px FireFox无法解释简写,   
  231.   
  232. 必须改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;   
  233.   
  234.   
  235. 22. 消除ul、ol等列表的缩进时   
  236.   
  237. 样式应写成:list-style:none;margin:0px;padding:0px;   
  238.   
  239. 其中margin属性对IE有效,padding属性对FireFox有效   
  240.   
  241.   
  242. 23. CSS透明   
  243.   
  244. IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。   
  245.   
  246. FF:opacity:0.6。   
  247.   
  248.   
  249. 24. CSS圆角   
  250.   
  251. IE:不支持圆角。   
  252.   
  253. 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;。   
  254.   
  255. 25. CSS双线凹凸边框   
  256.   
  257. IE:border:2px outset;。   
  258.   
  259. FF: -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值