标题 | css与javascript跨浏览器兼容性总结 |
内容 | 一、css样式兼容性 1. float闭合(clearing float) 网页在某些浏览器上显示错位很多时候都是因为使用了float浮动而没有真正闭合,这也是div无法自适应高度的一个原因。如果父div没有设float而其子div却设了float的话,父div无法包住整个子div,这种情况一般出现在一个父div下包含多个子div。解决办法: 1) 给父div也设上float 2) 在所有子div后新加一个空div(目前ext是这么做的),比如: .parent{width:100px;} .son1{float:left;width:20px;} .son2{float:left;width:80px;} .clear{clear:both;margin:0;parding0;height:0px;font-size:0px;} <div class=parent> <div class=son1></div> <div class=son2></div> <div class=clear></div> </div> 3) 万能 float 闭合 将以下代码加入global css 中,给需要闭合的div加上 class=”clearfix” 即可,屡试不爽。 <style> /* clear fix */ .clearfix:after { content:.; display:block; height:0; clear:both; visibility:hidden; } .clearfix { display:inline-block; } /* hide from ie mac */ .clearfix {display:block;} /* end hide from ie mac */ /* end of clearfix */ </style> :after(伪对象),设置在对象后发生的内容,通常和content配合使用,ie不支持此伪对象,非ie 浏览器支持,所以并不影响到ie/win浏览器。这种的最麻烦。 4) overflow:auto 只要在父div的css中加上overflow:auto就搞定。举例: .parent{width:100px;overflow:auto} .son1{float:left;width:20px;} .son2{float:left;width:80px;} <div class=parent> <div class=son1></div> <div class=son2></div> </div> 原理是,外围元素之所以不能很好的延伸,问题出在了overflow上,因为overflow不可见(见w3c的解释)。现在只要将给外围元素添 加一个“overflow:auto”,就可以解决问题,结果是除了ie,真的可以解决。下来就要解决ie的问题了,再加上“_height:1%”,这个问题就完全解决了。我试了一下,其实不加_height:1%“在ie下也行,留着吧。 2.截字省略号 .hh { -o-text-overflow:ellipsis; text-overflow:ellipsis; white-space:nowrap; overflow:hidden; } 这个是在越出长度后会自行的截掉多出部分的文字,并以省略号结尾。技术是好技术,很多人都喜欢乱用,但注意firefox并不支持。 <meta http-equiv=x-ua-compatible content=ie=7 /> 页面加上这句就可以让页面兼容ie7了 供参考! 给大家提醒一个漂浮需要注意的问题 注意设置div宽和高 注意设置overflow:hidden; 注意闭合针对火狐 父div样式display:inline-block; 3. cursor:hand和cursor:pointer firefox不支持hand,但ie支持pointer 解决方法:统一使用pointer 4. css透明 几款浏览器对透明度的支持方式各不相同,为了保证在ie, firefox, chrome, safari等主流浏览器下都能正常显示透明度的效果,我们可以定义一个透明度的class,因为一写就要写3条,省的每次都复制来复制去了。 具体代码如下: .transparent{ filter:alpha(opacity=60); /*支持 ie 浏览器*/ -moz-opacity:0.6; /*支持 firefox 浏览器*/ opacity:0.6; /*支持 chrome, opera, safari 等浏览器*/ } 5.css中的width和padding 在ie7和ff中width宽度不包括padding,在ie6中包括padding。 二、javascript兼容 1. children与childnodes ie提供的children、childnodes和firefox下的childnodes的行为是有区别的,firefox下childnodes会把换行和空白字符都算作父节点的子节点,而ie的childnodes和children不会。比如: <div id=dd> <div>yizhu2000</div> </div> id为dd的div在ie下用childnodes查看,其子节点数为1,而ff下为三,我们可以从firefox的dom查看器里面看到他的childnodes为[n , div, n]。 要在firefox下模拟children的属性我们可以这样做: if (typeof(htmlelement) != undefined && !window.opera) { htmlelement.prototype.__definegetter__(children, function() { for (var a = [], j = 0, n, i = 0; i < this.childnodes.length; i++) { n = this.childnodes[i]; if (n.nodetype == 1) { a[j++] = n; if (n.name) { if (!a[n.name]) a[n.name] = []; a[n.name][a[n.name].length] = n; } if (n.id) a[n.id] = n; } } return a; }); } 2. firefox和ie的事件 window.event只能在ie下使用,而不能用在firefox下,这是因为firefox的event只能在事件发生的现场使用。 firefox必须从源处加入event作参数传递。ie忽略该参数,用window.event来读取该event。 比方说下面这个在ie下获得鼠标位置的方法: <button onclick=onclick() >获得鼠标点击横坐标</button> <script type=text/javascript> function onclick(){ alert(event.clientx); } </script> 需要改成 <button onclick=onclick(event)>获得outerhtml</button> <script type=text/javascript> function onclick(event){ event = event || window.event; alert(event.clientx); } </script> 才能在两种浏览器下使用 3.html对象获取问题 firefox获取方式document.getelementbyid(idname) ie使用document.idname或者document.getelementbyid(idname) 解决办法:统一使用document.getelementbyid(idname); 4. const问题 在firefox下,可以使用const关键字或var关键字来定义常量; ie下,只能使用var关键字来定义常量; 解决方法:统一使用var关键字来定义常量。 5.frame问题 以下面的frame为例: <frame src=xxx.html id=frameid name=framename /> a)访问frame对象 ie:使用window.frameid或者window.framename来访问这个frame对象,frameid和framename可以同名; firefox:只能使用window.framename来访问这个frame对象; 另外,在ie和firefox中都可以使用window.document.getelementbyid(frameid)来访问这个frame对象; b) 切换frame内容 在 ie和firefox中都可以使用window.document.getelementbyid(testframe).src = xxx.html或window.framename.location = xxx.html来切换frame的内容; 如果需要将frame中的参数传回父窗口(注意不是opener,而是parent),可以在frame中使用parent来访问父窗口。例如: parent.document.form1.filename.value=aqing; 6. body问题 firefox的body在body标签没有被浏览器完全读入之前就存在;而ie的body则必须在body标签被浏览器完全读入之后才存在; 7. firefox与ie的父元素(parentelement)的区别 ie:obj.parentelement firefox:obj.parentnode 解决方法:因为firefox与ie都支持dom,因此全部使用obj.parentnode 8.innertext的问题 innertext在ie中能正常工作,但是innertext在firefox中却不行,需用textcontent; 解决方法: if (navigator.appname.indexof(explorer) > -1) { document.getelementbyid('element').innertext = my text; } else { document.getelementbyid('element').textcontent = my text; } 9.ajax获取xmlhttp的区别 var xmlhttp; if (window.xmlhttprequest) { xmlhttp = new xmlhttprequest(); } elseif (window.activexobject) { // ie的获取方式 xmlhttp = new activexobject(microsoft.xmlhttp); } 注意:在ie中,xmlhttp.send(content)方法的content可以为空,而firefox则不能为null,应该用send(),否则会出现411错误。 |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。