IE与Firefox的CSS兼容问题整理 |
1.DOCTYPE 影响 CSS 处理 9nWM9y http://blog.numino.net/ 2.FF: div 设置 margin-left, margin-right 为 auto 时已经居中, IE 不行 iO70ei http://blog.numino.net/ 3.FF: body 设置 text-align 时, div 需要设置 margin: auto(主要是 margin-left,margin-right) 方可居中 0FjM20 http://blog.numino.net/ 4.FF: 设置 padding 后, div 会增加 height 和 width, 但 IE 不会, 故需要用 !important 多设一个 height 和 width rM2OfT http://blog.numino.net/ 5.FF: 支持 !important, IE 则忽略, 可用 !important 为 FF 特别设置样式,值得注意的是,一定要将xxxx !important 这句放置在另一句之上 s46Vw1 http://blog.numino.net/ 6.div 的垂直居中问题: vertical-align:middle; 将行距增加到和整个DIV一样高 line-height:200px; 然后插入文字,就垂直居中了。缺点是要控制内容不要换行 U9Ufzn http://blog.numino.net/ 7.cursor: pointer 可以同时在 IE FF 中显示游标手指状, hand 仅 IE 可以 xyEcK4 http://blog.numino.net/ 8.FF: 链接加边框和背景色,需设置 display: block, 同时设置 float: left 保证不换行。参照 menubar, 给 a 和 menubar 设置高度是为了避免底边显示错位, 若不设 height, 可以在 menubar 中插入一个空格。 hnOEAC http://blog.numino.net/ 9.在firefox和IE中的BOX模型解释不一致导致相差2px解决方法:div{margin:30px!important;margin:28px;} 9LYe9K http://blog.numino.net/ 注意这两个margin的顺序一定不能写反,据阿捷的说法!important这个属性IE不能识别,但别的浏览器可以识别。所以在IE下其实解释成这样:div{maring:30px;margin:28px} IOUj1a http://blog.numino.net/ 重复定义的话按照最后一个来执行,所以不可以只写margin:XXpx!important; 2H9Hst http://blog.numino.net/ 10.IE5 和IE6的BOX解释不一致 Sga9gB http://blog.numino.net/ IE5下div{width:300px;margin:0 10px 0 10px;} 0JA29S http://blog.numino.net/ div的宽度会被解释为300px-10px(右填充)-10px(左填充)最终div的宽度为280px,而在IE6和其他浏览器上宽度则是以 300px+10px(右填充)+10px(左填充)=320px来计算的。这时我们可以做如下修改 div{width:300px!important;width /**/:340px;margin:0 10px 0 10px} v4Oudw http://blog.numino.net/ 关于这个/**/是什么我也不太明白,只知道IE5和firefox都支持但IE6不支持,如果有人理解的话,请告诉我一声,谢了!:) Xn5qA5 http://blog.numino.net/ 11.ul标签在Mozilla中默认是有padding值的,而在IE中只有margin有值所以先定义ul{margin:0;padding:0;}就能解决大部分问题。 BSN4Ge http://blog.numino.net/ 常见兼容问题2: 41LOsB http://blog.numino.net/ IE box-model这个臭名昭著的bug存在于IE6/Win以前的每一只版本,这个虫子直到tantak发布了流传最为广泛的那个hack才开始被驯服 7KS9qh http://blog.numino.net/ http://www.tantek.com/CSS/Examples/boxmodelhack.html Zp3RKf http://blog.numino.net/ IE5.X/win对box-model的解析是一样的,他们认为width包括了边框(border)和补白(padding),幸运的是这个情况在 IE6中有了好转,但是IE6在向后兼容的同时也包容了以前的错误,IE6其实有两个核心,在旧的页面前他仍旧表现出对错误的宽容,只有在文档中严格地加 上文档类型(DOCTYPE)声明,IE6才能够接受正确的box-model P7b1O7 http://blog.numino.net/ 所以,tantak的hack必须和正确的DOCTYPE同时包含在文档中才能够正常工作 pX16xT http://blog.numino.net/ div.content { P7UNX9 http://blog.numino.net/ width:400px; //这个是错误的width,所有浏览器都读到了 h3JJ3C http://blog.numino.net/ voice-family: “\”}\””; //IE5.X/win忽略了”\”}\””后的内容 p9wG03 http://blog.numino.net/ voice-family:inherit; bGmUA0 http://blog.numino.net/ width:300px; //包括IE6/win在内的部分浏览器读到这句,新的数值(300px)覆盖掉了旧的 NEaMoV http://blog.numino.net/ } 4dwIF2 http://blog.numino.net/ html>body .content { //html>body是CSS2的写法 YVuFk4 http://blog.numino.net/ width:300px; //支持CSS2该写法的浏览器有幸读到了这一句 1Va6H7 http://blog.numino.net/ } f2phHs http://blog.numino.net/ 现在回到主题,我们经常看到!important和(空格)/**/:组合在一起使用,这个写法有什么奥妙呢? K5w4MU http://blog.numino.net/ 看刚才那个写法,我这里可以提供另一种写法也能达到这样的效果 L866Hy http://blog.numino.net/ div.content { 1bbn39 http://blog.numino.net/ width:300px !important; //这个是正确的width,大部分支持!important标记的浏览器使用这里的数值 cwi7ui http://blog.numino.net/ width(空格)/**/:400px; //IE6/win不解析这句,所以IE6/win仍然认为width的值是300px;而IE5.X/win读到这句,新的数值(400px)覆盖掉了旧的,因为!important标记对他们不起作用 yJoFsr http://blog.numino.net/ } OY11KD http://blog.numino.net/ html>body .content { //html>body是CSS2的写法 HSJ5aa http://blog.numino.net/ width:300px; //支持CSS2该写法的浏览器有幸读到了这一句 P33NyV http://blog.numino.net/ } R6G7iO http://blog.numino.net/ 同样,这个方法仍必须依靠正确的文档类型声明才能够正常工作,原因在前面已经说过。 N2IOIx http://blog.numino.net/ 文档类型声明就像一个开关,打开向后兼容的未来,而错误使用的话,就是一个Pandora box x0h88Q http://blog.numino.net/ 注意事项: T1CpcU http://blog.numino.net/ 1、float的div一定要闭合。 uvTiot http://blog.numino.net/ 例如:(其中floatA、floatB的属性已经设置为float:left;) NeV5IA http://blog.numino.net/ < #div id=”floatA” > v21EZT http://blog.numino.net/ < #div id=”floatB” > 4JWCE2 http://blog.numino.net/ < #div id=”NOTfloatC” > DxYP0h http://blog.numino.net/ 这里的NOTfloatC并不希望继续平移,而是希望往下排。 P5NEpM http://blog.numino.net/ 这段代码在IE中毫无问题,问题出在FF。原因是NOTfloatC并非float标签,必须将float标签闭合。 Ro0sK4 http://blog.numino.net/ 在 v9E4AP http://blog.numino.net/ < #div class=”floatB”> zoH1Qe http://blog.numino.net/ < #div class=”NOTfloatC”> Aqei8a http://blog.numino.net/ 之间加上 v9lI5y http://blog.numino.net/ < #div class=”clear”> RsSeid http://blog.numino.net/ 这个div一定要注意声明位置,一定要放在最恰当的地方,而且必须与两个具有float属性的div同级,之间不能存在嵌套关系,否则会产生异常。 42GZ3A http://blog.numino.net/ 并且将clear这种样式定义为为如下即可: PWG84l http://blog.numino.net/ .clear{ j42YW3 http://blog.numino.net/ clear:both;} unEh8Q http://blog.numino.net/ 此外,为了让高度能自动适应,要在wrapper里面加上overflow:hidden; qcpj0D http://blog.numino.net/ 当包含float的box的时候,高度自动适应在IE下无效,这时候应该触发IE的layout私有属性(万恶的IE啊!)用zoom:1;可以做到,这样就达到了兼容。 yGHS0s http://blog.numino.net/ 例如某一个wrapper如下定义: 6dz4I6 http://blog.numino.net/ .colwrapper{ 4wZYMv http://blog.numino.net/ overflow:hidden; 24Zoob http://blog.numino.net/ zoom:1; sjgA0r http://blog.numino.net/ margin:5px auto;} I7iZ2w http://blog.numino.net/ 2、margin加倍的问题。 xoyXTb http://blog.numino.net/ 设置为float的div在ie下设置的margin会加倍。这是一个ie6都存在的bug。 b2624z http://blog.numino.net/ 解决方案是在这个div里面加上display:inline; 9Po73S http://blog.numino.net/ 例如: Na5gjN http://blog.numino.net/ < #div id=”imfloat”> sQaUJA http://blog.numino.net/ 相应的css为 i28WzS http://blog.numino.net/ #IamFloat{ kiPt29 http://blog.numino.net/ float:left; 44z1gW http://blog.numino.net/ margin:5px;/*IE下理解为10px*/ 0nyeGS http://blog.numino.net/ display:inline;/*IE下再理解为5px*/} vT9RMJ http://blog.numino.net/ 3、关于容器的包涵关系 NpoKTl http://blog.numino.net/ 很多时候,尤其是容器内有平行布局,例如两、三个float的div时,宽度很容易出现问题。在IE中,外层的宽度会被内层更宽的div挤破。一定要用Photoshop或者Firework量取像素级的精度。 XRPhCe http://blog.numino.net/ 4、关于高度的问题 yGtb9F http://blog.numino.net/ 如果是动态地添加内容,高度最好不要定义。浏览器可以自动伸缩,然而如果是静态的内容,高度最好定好。(似乎有时候不会自动往下撑开,不知道具体怎么回事) QV1zvs http://blog.numino.net/ 5、最狠的手段 - !important; 9hlNbS http://blog.numino.net/ 如果实在没有办法解决一些细节问题,可以用这个方法.FF对于”!important”会自动优先解析,然而IE则会忽略.如下 0rwG6N http://blog.numino.net/ .tabd1{ O00eYW http://blog.numino.net/ background:url(/res/images/up/tab1.gif) no-repeat 0px 0px !important; /*Style for FF*/ dnsIHG http://blog.numino.net/ background:url(/res/images/up/tab1.gif) no-repeat 1px 0px; /* Style for IE */} jy4G3E http://blog.numino.net/ 值得注意的是,一定要将xxxx !important 这句放置在另一句之上,上面已经提过。 8g525K http://blog.numino.net/ 其他: j7Hmtn http://blog.numino.net/ 注:IE都能识别*;标准浏览器(如Firefox,Opera,Netscape)不能识别*;IE6能识别*,但不能识别 !important,IE7能识别*,也能识别!important;FF不能识别*,但能识别!important; hj63W4 http://blog.numino.net/ 写两句代码来控制一个属性,区别Firefox与IE6: LhxmBV http://blog.numino.net/ background:orange;*background:blue; iEsgDp http://blog.numino.net/ //这一句代码写出来时,你用firefox或其它非IE浏览时,会发现,写了该代码的区域背景是橙色的,如果用IE浏览,却是蓝色的,这是因为IE都能识别*;标准浏览器(如Firefox,Opera,Netscape)不能识别*; mFFQlR http://blog.numino.net/ 写两句代码来控制一个属性,区别IE7与IE6: aEjwDM http://blog.numino.net/ background:green !important;background:blue; IOL0rj http://blog.numino.net/ //这一句代码写出来时,你用IE7浏览,会发现,写了该代码的区域背景是绿色的,如果用IE6浏览,却是蓝色的,这是因为IE7能识 别!important*,一但识别了,就执行,忽略了后面的那一句,但IE6却不能识别!important,所以前面部分跳过,直接执行了后半部份。 5d3Rmt http://blog.numino.net/ 写两句代码来控制一个属性,区别Firefox与IE: 7Ab4WZ http://blog.numino.net/ background:orange; *background:green; r0Pwr2 http://blog.numino.net/ //这一句代码写出来时,你用Firefox浏览,会发现背景是橙色的,而IE里却是绿色的,很简单,因为Firefox不能识别*,而IE6,IE7都可以识* ykjiN7 http://blog.numino.net/ 写三句代码来控制一个属性,区别Firefox,IE7,IE6: NqRFx3 http://blog.numino.net/ background:orange;*background:green !important;*background:blue; G520dp http://blog.numino.net/ //这一句会使在Firefox在,背景呈橙色,IE7中为绿色,IE6中为蓝色,道理和前面一样,Firefox不能识别*,所以后面两句都不 执行,直接执行第一句,IE7当然也能执行第一行代码,但是因为第二句,他也能识别,所以就执行了第二句代码,把前面的效果给过滤了,而最后一句,IE7 是不能识别的。IE6不能识别!imprtant,本来运行了第一句代码了,第二句不能识别,那就理所当然的执行了最后一句。 MrZiD5 http://blog.numino.net/ ☆ 关于调试JS的tip gnk5k9 http://blog.numino.net/ 调试js时,经常受缓存的影响,页面不会加载最新的代码,为此解决如下: R6M6QT http://blog.numino.net/ 在新页面里打开原来的地址,关闭原来的页面,ok了. 2Kft6k http://blog.numino.net/ 以上方法在IE和FF下都有效. xAFfI2 http://blog.numino.net/ ☆ IE -> firefox js类 W24Q8d http://blog.numino.net/ △ document.all -> document.getElementById mpByyh http://blog.numino.net/ 并且控件尽量用id,而不是name标识 y7Z4Tr http://blog.numino.net/ 为了兼容旧代码,参考下面的函数, 把document.all 替换为 document_all即可 ai6hF8 http://blog.numino.net/ function document_all(objName,doc){ DwsgtP http://blog.numino.net/ if (!doc) doc = document; JmZOA1 http://blog.numino.net/ var obj; 5cT6hu http://blog.numino.net/ obj = doc.getElementById(objName); JB3iNy http://blog.numino.net/ if (!obj) obj = doc.getElementsByName(objName)[0]; 9kSv04 http://blog.numino.net/ return obj; tjvygy http://blog.numino.net/ } Wg3KR4 http://blog.numino.net/ 提示: kPQ4bV http://blog.numino.net/ 如果控件只有name,没有id, 用getElementById时: uRd0sl http://blog.numino.net/ IE:也可以找到对象 t67KrR http://blog.numino.net/ FF:返回NULL E01SuU http://blog.numino.net/ △ 判断对象是否是object的方法应该为 9hTDyV http://blog.numino.net/ if( typeof 对象变量 == "object") rrI836 http://blog.numino.net/ 而不是 if(对象变量 == "[object]") cvbTL3 http://blog.numino.net/ △ eval(对象名称) -> document.getElementById xX42t0 http://blog.numino.net/ FF支持eval函数 1rLAc9 http://blog.numino.net/ △ 通过id直接调用对象 1N2iIP http://blog.numino.net/ name.value = "" L336Si http://blog.numino.net/ 改为 sRuguf http://blog.numino.net/ document.getElementById("name").value = "" I751JX http://blog.numino.net/ △ window.event 的改造, firefox和opera会在调用函数时传入event对象, MK7XF6 http://blog.numino.net/ ie用全局对象window.event,可以修改事件处理函数,接收event对象 kWtOyf http://blog.numino.net/ function editmask_keydown(event){ 6ujpXT http://blog.numino.net/ event = event || window.event; htk4Yg http://blog.numino.net/ 这样定义对系统改动比较大, 所以从底层转换处理,可能更好一些 30Bz0E http://blog.numino.net/ 参考 ie_to_w3c.js 84ZoVr http://blog.numino.net/ △ X6MCwm http://blog.numino.net/ mX = event.x ? event.x : event.pageX; 6iBTz8 http://blog.numino.net/ △ 不支持onpropertychange事件 tbw275 http://blog.numino.net/ △ obj.insertAdjacentElement("beforeBegin",objText); 97Kw6a http://blog.numino.net/ 改为用 3je4H8 http://blog.numino.net/ obj.parentNode.insertBefore(objText,obj); XR00dp http://blog.numino.net/ △ createElement不支持HTML代码 mQV2MW http://blog.numino.net/ 用document.write(esHTML); 可以解决一部分情况 YKqwEu http://blog.numino.net/ △ innerText -> textContent bRoMwg http://blog.numino.net/ △ 对象名称中的$改为_ 5Ya8mu http://blog.numino.net/ objName = "t1$spin" Q5Ti1a http://blog.numino.net/ 改为 vX4IJU http://blog.numino.net/ objName = "t1_spin" M4Q2pc http://blog.numino.net/ △ 事件名称和注册方式的变化 Zf5YSs http://blog.numino.net/ this.selYear.addEventListener("blur", myBlur, false); C9TLfe http://blog.numino.net/ △ FF里设置Attribute为某个对象,然后再取出来,这时候对象的属性都丢失了? 1549q0 http://blog.numino.net/ objText.setAttribute("dropdown_select",obj); f5wN5r http://blog.numino.net/ alert(obj.id) //正确的名字 g0cZ1a http://blog.numino.net/ var objxx = objText.getAttribute("dropdown_select"); jcBWrD http://blog.numino.net/ alert(objxx.id) //null iBShPJ http://blog.numino.net/ 在IE下没有问题, FF对setAttribute来说,第2个参数都是字符串型的!!! 4wq6x4 http://blog.numino.net/ 所以如果第2个参数是对象时,相当于调用对象的 toString() 方法了 6k9xKZ http://blog.numino.net/ 解决的方法是用下面的调用方式: Pbbiyt http://blog.numino.net/ objText.dropdown_select = obj; 0x8St2 http://blog.numino.net/ obj = objText.dropdown_select Y1Hcr3 http://blog.numino.net/ 但是有资料说对用户自定义的属性,必须用setAttribute, 看来什么都要实践证实一下! zM2Rjg http://blog.numino.net/ 为了区分系统的属性和用户自定义的属性, 是否在命名上做点研究? AbFHqx http://blog.numino.net/ obj.custom_dropdown_select XQ97VU http://blog.numino.net/ △ className -> class 6r3r0A http://blog.numino.net/ FF下用class代替IE下的className c9FC2R http://blog.numino.net/ 由于class是关键字, 所以需要用 setAttribute/getAttribute才行 Ynr9KQ http://blog.numino.net/ △ 在html里定义的属性,必须用getAttribute才行 aV83WP http://blog.numino.net/ <td id="TD1" isOBJ="true"> 8BlA7u http://blog.numino.net/ 获取时: 7MBxWJ http://blog.numino.net/ document.getElementByID("TD1").isOBJ 总返回 undefined mrYhOm http://blog.numino.net/ 应该用: l47n16 http://blog.numino.net/ document.getElementByID("TD1").getAttribute("isOBJ") th2vsK http://blog.numino.net/ △ FF里select控件不再是:总是在顶端显示 a1pa5H http://blog.numino.net/ 所以需要设置控件的zIndex v4vF70 http://blog.numino.net/ △ 对于if ( vars ) 下面的值用于判断是等同的 41z8CV http://blog.numino.net/ undefined E7zDoB http://blog.numino.net/ null PENaip http://blog.numino.net/ false 2y4w65 http://blog.numino.net/ 0 6NLsM6 http://blog.numino.net/ △ 如果FF调用obj.focus(); 报错,请尝试改为: 8H2Nrj http://blog.numino.net/ window.setTimeout( function(){ obj.focus(); }, 0); Pp2fCP http://blog.numino.net/ 现在的方案是在失去焦点前判断 GaqvBo http://blog.numino.net/ △ 关于在控件的blur事件里判断下一个获得焦点的控件的tip, 类似于Powerbuilder的item focus changing事件 6cW2zO http://blog.numino.net/ 用途举例: 对dropdown控件里的text控件, CYuro3 http://blog.numino.net/ 如果下一个获得焦点的对象是selection,则不做录入控制,如果不是,则提示,并设置焦点为text 6mCwEf http://blog.numino.net/ 经测试: 1SD3SJ http://blog.numino.net/ IE: blur发生在focus后 cJ42A2 http://blog.numino.net/ FF: blur发生在focus前调用 irzHH2 http://blog.numino.net/ IE: 在onbeforedeactivate事件里用activeElement可以获得下一个对象 Yaa51p http://blog.numino.net/ FF: blur事件发生在focus前面,而FF又没有中间事件 zVWVB2 http://blog.numino.net/ 下面的都不行! HSmxX3 http://blog.numino.net/ target 483wdT http://blog.numino.net/ currentTarget ni8g6J http://blog.numino.net/ relativeTarget YNbu1s http://blog.numino.net/ 看来需要这样处理! n4Eaim http://blog.numino.net/ 注册所有控件的onblur,设置lastElement 3207QN http://blog.numino.net/ 在focus里处理上一个? 也不行啊! O57SB5 http://blog.numino.net/ 能否模拟自己的itemfocuschanging事件? 8UP2gN http://blog.numino.net/ IE: onbeforedeactivate 1dXt35 http://blog.numino.net/ FF: 抢先注册onfocus和onblur, 在里面设置activeElement Pj0s7e http://blog.numino.net/ 在onfocus里设置activeElement后, yWa88m http://blog.numino.net/ 调用onbeforedeactivate gGk7xp http://blog.numino.net/ 如果onbeforedeactivate返回false,则设置focus为原来的控件! hMka22 http://blog.numino.net/ 在控件失去焦点前,设置lastObj=this pimQq6 http://blog.numino.net/ 在控件获得焦点时,先判断lastObj是什么 OZ65WQ http://blog.numino.net/ △ FF不能处理alert造成的死循环情况 Rp7445 http://blog.numino.net/ △ FF下,keyCode是只读的, 那把回车转换为tab怎么办? 在录入时进行转换怎么办?? N618Vh http://blog.numino.net/ 变通的方法是: dkdaeY http://blog.numino.net/ 1. 回车跳转 -> 自己写跳转处理代码. d9Kp92 http://blog.numino.net/ 2. 在能够录入的控件里, 5W69j0 http://blog.numino.net/ 把选中的部分替换为新录入的内容: var text = String.fromCharCode(event.keyCode); J7EgWt http://blog.numino.net/ 同时阻止按键事件上传, 调用: event.preventDefault() Aq19Eo http://blog.numino.net/ △ selection的难度较大,因为IE和FF没有接近的函数 p2mPfI http://blog.numino.net/ 为此专门做了一个对象来消除差异 0gO0rI http://blog.numino.net/ 感觉firefox的selection机制使用起来更简洁一些 Vf74QF http://blog.numino.net/ 这种模式是桥梁模式还是 83Xi6a http://blog.numino.net/ △ <button> 会被firefox解释为提交form或者刷新页面??? zd5h9k http://blog.numino.net/ 需要写标准<button type="button"> 1Jv8Th http://blog.numino.net/ △ 在firefox里, document.onfocus里获得不到实际获得焦点的控件? 02t5G8 http://blog.numino.net/ 为什么document.keydown可以呢? xiTNos http://blog.numino.net/ △ children -> childNode() iFSe02 http://blog.numino.net/ △ sytle.pixelHeight -> style.height duenPo http://blog.numino.net/ △ obj.attachEvent("onfocus", editmask_focus); 3TJh7l http://blog.numino.net/ obj.onfocus = editmask_focus; 5DNYGh http://blog.numino.net/ 上面2句竟然有区别! nLk7g8 http://blog.numino.net/ 如果用 attachEvent, 就不能用 this 关键字了! 8imnmF http://blog.numino.net/ 通用的做法是不用this, 而是用 event.srcElement O9k6f6 http://blog.numino.net/ △ fireevent不能用 rix0vs http://blog.numino.net/ ☆ IE -> firefox css类 v228mE http://blog.numino.net/ △ cursor:hand -> cursor:pointer 0Hq510 http://blog.numino.net/ △ expression firefox不支持 21L19j http://blog.numino.net/ 在IE下expression也非常消耗CPU,所以不应该使用!! b7etz2 http://blog.numino.net/ 为了达到较好的效果,应该建立自己的expression to javascript的处理函数 IswlWZ http://blog.numino.net/ 这样在IE和FF里就都能用了. FO5pYY http://blog.numino.net/ △ FILTER firefox不支持
|
|