firefox与IE对js和CSS的区别

1. document.formName.item("itemName")的问题 
说明
    ie,可以使用document.formName.item("itemName")document.formName.elements["elementName"]; 
firefox,只能使用document.formName.elements["elementName"] 
解决方法:统一使用 

Js代码   收藏代码

1.  document.formName.elements["elementName"]  


2.集合类对象问题 
说明
    IE,可以使用()[]获取集合类对象;Firefox,只能使用[]获取集合类对象
解决方法:统一使用[]获取集合类对象。 
3.自定义属性问题 
说明
    IE,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox,只能使用getAttribute()获取自定义属性。 
解决方法
    统一通过getAttribute()获取自定义属性。 
4.eval("idName")问题 
说明
    IE,可以使用eval("idName")getElementById("idName")来取得ididNameHTML对象;Firefox下只能使用getElementById("idName")来取得ididNameHTML对象。 
解决方法
    统一用getElementById("idName")来取得ididNameHTML对象。 
5.变量名与某HTML对象ID相同的问题 
说明
    IE,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox,可以使用与HTML对象ID相同的变量名;IE下则不能。 
解决方法
    使用document.getElementById("idName")代替document.idName。最好不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义。 
6.const问题 
说明
    Firefox,可以使用const关键字或var关键字来定义常量;IE,只能使用var关键字来定义常量
解决方法:统一使用var关键字来定义常量。 
7.input.type属性问题 
说明
    IEinput.type属性为只读;但是Firefoxinput.type属性为读写。 
8.window.event问题 
说明
    window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefoxevent只能在事件发生的现场使用。Firefox必须从源处加入event作参数传递。ie忽略该参数,用window.event来读取该event 
解决方法
IE&Firefox: 
Submitted(event)"/> … 

Js代码   收藏代码

1.     

2.  function Submitted(evt) {   

3.  evt=evt?evt:(window.event?window.event:null);   

4.  }   


9.event.xevent.y问题 
说明
     IE,even对象有x,y属性,但是没有pageX,pageY属性;Firefox,even对象有pageX,pageY属性,但是没有x,y属性。 
解决方法
     使用mX(mX = event.x ? event.x : event.pageX;)来代替IE下的event.x或者Firefox下的event.pageX 
10.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的兼容性问题。 
11.window.location.href问题 
说明
     IE或者Firefox2.0.x,可以使用window.locationwindow.location.href;Firefox1.5.x,只能使用window.location 
解决方法
     使用window.location来代替window.location.href 
12.模态和非模态窗口问题 
说明
     IE,可以通过showModalDialogshowModelessDialog打开模态和非模态窗口;Firefox下则不能。 
解决方法
     直接使用window.open(pageURL,name,parameters)方式打开新窗口。 
如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口. 例如: 

Js代码   收藏代码

1.  var parWin = window.opener;  

2.  parWin.document.getElementById("C_Name").value = "john";  


13.frame问题 
以下面的frame为例: 
<frame src="xxx.html" id="frameId" name="frameName" /> 
(1)访问frame对象
IE:使用window.frameId或者window.frameName来访问这个frame对象。frameIdframeName可以同名。 
Firefox:只能使用window.frameName来访问这个frame对象。 
另外,在IEFirefox中都可以使用window.document.getElementById("frameId")来访问这个frame对象。 
(2)切换frame内容
IEFirefox中都可以使用window.document.getElementById("testFrame").src = "xxx.html"window.frameName.location = "xxx.html"来切换frame的内容。 
如果需要将frame中的参数传回父窗口(注意不是opener,而是parent frame),可以在frame中使用parent来访问父窗口。例如: 

Js代码   收藏代码

1.  parent.document.form1.filename.value="jquery";  


14.body问题 
Firefoxbodybody标签没有被浏览器完全读入之前就存在;而IEbody则必须在body标签被浏览器完全读入之后才存在。 
15. 事件委托方法 
IEdocument.body.onload = inject; //Function inject()在这之前已被实现 
Firefoxdocument.body.onload = inject(); 
16. firefoxIE的父元素(parentElement)的区别 
IEobj.parentElement 
firefoxobj.parentNode 
解决方法
     因为firefoxIE都支持DOM,因此使用obj.parentNode是不错选择。 
17.cursor:hand VS cursor:pointer 
firefox不支持hand,但ie支持pointer 
解决方法: 统一使用pointer 
18.innerText问题 
     IE中能正常工作,但是innerTextfirefox中却不行. 需用textContent 
解决方法

Js代码   收藏代码

1.  if(navigator.appName.indexOf("Explorer") > -1){   

2.      document.getElementById('element').innerText = "my text";   

3.  else{   

4.      document.getElementById('element').textContent = "my text";   

5.  }  


19. 设置HTML标签的style 
     FireFox中设置HTML标签的style时,所有位置性和字体尺寸的值必须后跟px。这个ie也是支持的。 
20. table操作 
     ie,firefox以及其它浏览器对于 table 标签的操作都各不相同,在ie中不允许对tabletrinnerHTML赋值,使用js增加一个tr时,使用appendChild方法也不管用。 
解决方法: 
//table追加一个空行: 

Js代码   收藏代码

1.  var row = otable.insertRow(-1);   

2.  var cell = document.createElement("td");   

3.  cell.innerHTML = " ";   

4.  cell.className = "XXXX";   

5.  row.appendChild(cell);   


21. padding问题 
      padding 5px 4px 3px 1px; FireFox无法解释简写,必须改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px; 
22. 消除ulol等列表的缩进时 
      样式应写成:list-style:none;margin:0px;padding:0px;其中margin属性对IE有效,padding属性对FireFox有效。 
23. css透明 
IEfilter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60) 
FFopacity:0.6 
IE:filteralpha(opacity=10); 
firefox-moz-opacity:.10; 
24. 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; 
25. CSS双线凹凸边框 
IEborder:2px outset; 
FF -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080; 
26. selectoptions集合操作 
      枚举元素除了[]外,SelectName.options.item()也是可以的, 另外SelectName.options.length, SelectName.options.add/remove都可以在两种浏览器上使用。 
27. XMLHTTP的区别 

Js代码   收藏代码

1.  //mf   

2.  if (window.XMLHttpRequest) //mf   

3.  {   

4.  xmlhttp=new XMLHttpRequest()   

5.  xmlhttp.   

6.  xmlhttp.open("GET",url,true)   

7.  xmlhttp.send(null)   

8.  }   

9.  //ie   

10. else if (window.ActiveXObject) // code for IE   

11. {   

12. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")   

13.     if (xmlhttp)   

14.     {   

15.     xmlhttp.   

16.     xmlhttp.open("GET",url,true)   

17.     xmlhttp.send()   

18.     }   

19. }   

20. }   


28. innerHTML的区别 
Firefox不支持innerHTML, 解决办法可以如下 

Js代码   收藏代码

1.  rng = document.createRange();   

2.  el = document.getElementById(elementid);   

3.  rng.setStartBefore(el);   

4.  htmlFrag = rng.createContextualFragment(content);   

5.  while (el.hasChildNodes()) //清除原有内容,加入新内容   

6.         el.removeChild(el.lastChild);   

7.  el.appendChild(htmlFrag);   


29. imgsrc刷新问题 
IE下可以用<img id="pic" οnclick= "this.src= &apos;aa.php&apos;" src="aa.php" style="cursor: pointer"/> 可以刷新图片,但在FireFox下不行。主要是缓存问题,在地址后面加个随机数就解决了。编辑onclick事件代码如下: "this.src=this.src+'?'+Math.random()" 
30.!important 
    FF对于"!important"会自动优先解析,IE7已支持,然而IE6则会忽略。 

Html代码   收藏代码

1.  .tabd{  

2.  background:url(/res/images/up/tab1.gif) no-repeat 0px 0px !important; /*Style for FF and IE7*/  

3.  background:url(/res/images/up/tab1.gif) no-repeat 1px 0px; /* Style for IE6 */  

4.  }  


31.针对firefox ie6 ie7css样式 
     *+html *html IE特有的标签, firefox 暂不支持.*+html 又为 IE7特有标签。 

Html代码   收藏代码

1.  #color1 { color: #333; } /* FireFox */   

2.  * html #color1 { color: #666; } /* IE6 */   

3.  *+html #color1 { color: #999; } /* IE7 */   


    那么在firefox下字体颜色显示为#333IE6下字体颜色显示为#666IE7下字体颜色显示为#999。注意它们的书写顺序。 
    *+html IE7HACK 必须保证HTML顶部有如下声明: 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
32.margin加倍的问题 
    设置为floatdivie下设置的margin会加倍。这是一个ie6都存在的bug 
     解决方案是在这个div里面加上display:inline; 

Html代码   收藏代码

1.  <style type="text/css">  

2.  #IamFloat{  

3.    float:left;  

4.    margin:5px;/*IE下理解为10px*/  

5.    display:inline;/*IE下再理解为5px*/  

6.  }  

7.  </style>  

8.  <div id="imfloat"></div>  


33.mozilla firefoxIE中的BOX模型解释不一致导致相差2px解决方法 

Html代码   收藏代码

1.  div{  

2.    margin:30px!important;  

3.    margin:28px;  

4.  }  


    !important这个属性IE不能识别,但别的浏览器可以识别。所以在IE下其实解释成这样:div{maring:30px;margin:28px},重复定义的话按照最后一个来执行。 
34.禁止选取网页内容 
IE中一般用jsobj.onselectstart=function(){return false;} 
firefoxCSS:-moz-user-select:none 
35.捕获事件 
IEobj.setCapture() obj.releaseCapture() 
Firefox 
document.addEventListener("mousemove",mousemovefunction,true); 
document.removeEventListener("mousemove",mousemovefunction,true); 
36.document.documentElement 
    HTML4.0标准下用document.body,在XHTML标准下就要换成document.documentElementXHTMLdocument.body仅仅表示body对象,而不能代表文档内容所渲染的整个表面。HTMLdocument.body.clientHeight表示浏览器的可视高度,XHTML下则是document.documentElement.clientHeightdocument.body.clientHeightXHTML下仅表示body的可视高度。在HTML4.0下用document.body.scrollTop;而在XHTML下则是document.documentElement.scrollTop,之前的document.body.scrollTop是恒为0的。 

Js代码   收藏代码

1.  //可以这样兼容:  

2.  function getBodyObj(){  

3.     return (document.documentElement) ? document.documentElement : document.body ;  

4.  }  


37.min-height最小高度的实现(兼容IE6IE7FF 
    最小高度min-height是一个非常有用的属性,当容器的内容较少时,能保持一个最小的高度,以免破坏了布局或UI设计效果。而当容器内的内容增加的时候,容器能够自动的伸展以适应内容的变化。 
    min-height属性并非所有浏览器都兼容,主要问题还是出现在IE6 
    IE6对于overflow的特殊实现,给我们实现min-height提供了一个思路,所以产生了以下兼容IE6IE7FF浏览器的min-height写法: 

Html代码   收藏代码

1.  #main-container {  

2.      background:#ccc;  

3.      min-height:100px;   

4.      height:auto !important;   

5.      height:100px;   

6.      overflow:visible;  

7.  }  


38.float闭合(clear:both) 
    如果上面用float控制了nDIV,下面ie会自动检测自动排列,ff则可能很不老实,到处乱动。 
解决: 
     float结束后的下一个标签加clear:both;以结束float的控制。 
     将以下代码加入Global CSS ,给需要闭合的div加上 class="clearfix" 即可! 

Html代码   收藏代码

1.  /* Clear Fix */   

2.  .clearfix:after{   

3.     content:".";   

4.     display:block;   

5.     height:0;   

6.     clear:both;   

7.     visibility:hidden;   

8.  }   

9.  .clearfix{   

10.    display:inline-block;   

11. }   

12. /* Hide from IE Mac */   

13. .clearfix {display:block;}   

14. /* End hide from IE Mac */   

15. /* end of clearfix */   


:after(伪对象)--设置在对象后发生的内容,通常和content配合使用,IE不支持此伪对象,非Ie 浏览器支持。 
39.最大/小宽度问题 
     min-width,max-width只是ff的命令,如何让ie实现同样的效果
解决: 
     ie不认识min-max-,实际ie认为min-widthmax-widthwidth效果一样,可以用下面方法解决

Html代码   收藏代码

1.  #cctext{  

2.     min-width: 700px;   

3.     max-width: 1000px;  

4.     width:expression(document.body.clientWidth<700 ? "700px" : document.body.clientWidth>1000 ? "1000px" : "auto");//只有ie认得   

5.  }  


40.块元素居中对齐 

Html代码   收藏代码

1.  body { text-align: center }/*for IE6*/  

2.  #content { text-align: left; width: 700px; margin: 0 auto }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值