Home > Web Front-end > JS Tutorial > Detailed explanation of js compatibility issues

Detailed explanation of js compatibility issues

小云云
Release: 2018-03-13 16:21:42
Original
2042 people have browsed it

This article mainly shares with you the detailed explanation of js compatibility issues,

1.HTML object acquisition problem

FireFox: document.getElementById("idName");

ie :document.idname or document.getElementById(“idName”).

Solution: Use document.getElementById(“idName”);

2.const problem

Note: Under Firefox, you can use the const keyword or the var keyword to define constants;

Under IE, you can only use the var keyword to define constants.

Solution: Use the var key uniformly Words to define constants.

3.Event.x and event.y issues

Explanation: Under IE, the event object has x, y attributes, but does not have pageX, pageY attributes;

Under Firefox, the event object has pageX, pageY attributes, but no x, y attributes.

Solution: Use mX (mX = event.x ? event.x : event.pageX;) instead event.x under IE or event.pageX under Firefox.

4.window.location.href problem

Explanation: Under IE or Firefox2.0.x, you can use window.location Or window.location.href;

Under Firefox1.5.x, you can only use window.location.

Solution: Use window.location instead of window.location.href.

5.Frame problem

Take the following frame as an example:


<frame  src=”xxx.html”  id=”frameId”  name=”frameName”  />
Copy after login


(1) Access the frame object:

IE: Use window.frameId or window.frameName to access this frame object. frameId and frameName can have the same name.

Firefox: You can only use window.frameName to access this frame object.

In addition, you can use window.document.getElementById("frameId") to access this frame in both IE and Firefox Object.

(2) Switch frame content:

You can use window.document.getElementById("testFrame").src = "xxx.html" or window in both IE and Firefox. frameName.location = "xxx.html" to switch the content of the frame.

If you need to pass the parameters in the frame back to the parent window (note that it is not the opener, but the parent frame), you can use parent in the frame Access the parent window. For example: parent.document.form1.filename.value=”Aqing”;

6. Modal and non-modal window issues

Note: Under IE, you can open the modal through showModalDialog and showModelessDialog Modal and non-modal windows; not available under Firefox.

Solution: Directly use window.open(pageURL,name,parameters) to open a new window.

If you need to pass parameters in the child window back to the parent window, you can use window.opener in the child window to access the parent window.

For example: var parWin = window.opener; parWin. document.getElementById(“Aqing”).value = “Aqing”;

7.The difference between the parent element (parentElement) of firefox and IE

IE: obj.parentElement

firefox:obj.parentNode

Solution: Since both firefox and IE support DOM, using obj.parentNode is a good choice.

8.document.formName.item("itemName") Problem

Problem Description: Under IE, you can use document.formName.item("itemName") or document.formName.elements ["elementName"]; under Firefox, you can only use document.formName.elements[" elementName"].

Solution: Use document.formName.elements["elementName"] uniformly.

9. Collection class object problem

Problem description: Under IE, you can use () or [] to obtain collection class objects; under Firefox, you can only use [] to obtain collection class objects.

Solution: Use [] uniformly to obtain collection objects.

10. Custom attribute problem

Problem description: Under IE, you can use the method of obtaining regular attributes to obtain custom attributes, or you can use getAttribute() to obtain custom attributes; under Firefox , custom attributes can only be obtained using getAttribute().

Solution: Uniformly obtain custom attributes through getAttribute().

11.Input.type attribute problem

Problem description: The input.type attribute under IE is read-only; but the input.type attribute under Firefox is read-write.

Solution: Do not modify the input.type attribute. If you must modify it, you can hide the original input first, and then insert a new input element at the same position.

12.event.srcElement problem

Problem description: Under IE, the even object has the srcElement attribute, but no target attribute; under Firefox, the even object has the target attribute, but no srcElement attribute.

Solution: Use srcObj = event.srcElement ?event.srcElement : event.target;

If you consider the 8th question, just use myEvent instead of event.

13.body loading problem

Problem description: Firefox's body object exists before the body tag is fully read by the browser; while IE's body object must be before the body tag is read. It does not exist until the browser has completely read it.

[Note] This issue has not been actually verified and will be modified after verification.

[Note] It has been verified that the above problem does not exist in IE6, Opera9 and FireFox2. A simple JS script can access all objects and elements that have been loaded before the script, even if the element has not been loaded.

14. Event delegation method

Problem description: Under IE, use document.body.onload = inject; where function inject() has been implemented before; under Firefox, use document .body.onload = inject();

Solution: Use document.body.onload=new Function('inject()'); or document.body.onload = function(){/* here Is the code*/}

[注意] Function和function的区别。

15.Table操作问题

问题说明:ie、firefox以及其它浏览器对于 table 标签的操作都各不相同,在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChild方法也不管用。

解决方法://向table追加一个空行:

var row = otable.insertRow(-1);var cell = document.createElement(“td”);cell.innerHTML = “”;cell.className = “XXXX”;row.appendChild(cell);[注] 由于俺很少使用JS直接操作表格,这个问题没有遇见过。建议使用JS框架集来操作table,如JQuery。

16.对象宽高赋值问题

问题说明:FireFox中类似obj.style.height = imgObj.height的语句无效。

Ø CSS

1.cursor:hand VS cursor:pointer

firefox不支持hand,但ie支持pointer

解决方法: 统一使用pointer

2.innerText在IE中能正常工作,但在FireFox中却不行.

需用textContent。

解决方法:

?
   if(navigator.appName.indexOf(“Explorer”)  >  -1){
document.getElementById(‘element&#39;).innerText  =  “my  text”;
}
else{
document.getElementById(‘element&#39;).textContent  =  “my  text”;
}
Copy after login

3.CSS透明

IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。

FF:opacity:0.6。

4.css中的width和padding

在IE7和FF中width宽度不包括padding,在Ie6中包括padding.
5.FF和IEBOX模型解释不一致导致相差2px

box.style{width:100;border 1px;}

ie理解为box.width = 100

ff理解为box.width = 100 + 1*2 = 102 //加上边框2px

解决方法:p{margin:30px!important;margin:28px;}

注意这两个margin的顺序一定不能写反, IE不能识别!important这个属性,但别的浏览器可以识别。所以在IE下其实解释成这样:p{maring:30px;margin:28px}

重复定义的话按照最后一个来执行,所以不可以只写margin:XXpx!important;

6.IE5 和IE6的BOX解释不一致

IE5下p{width:300px;margin:0 10px 0 10px;}

p 的宽度会被解释为300px-10px(右填充)-10px(左填充),最终p的宽度为280px,而在IE6和其他浏览器上宽度则是以 300px+10px(右填充)+10px(左填充)=320px来计算的。这时我们可以做如下修改 p{width:300px!important;width /**/:340px;margin:0 10px 0 10px}

7.ul和ol列表缩进问题

消除ul、ol等列表的缩进时,样式应写成:list-style:none;margin:0px;padding:0px;

经验证,在IE中,设置margin:0px可以去除列表的上下左右缩进、空白以及列表编号或圆点,设置padding对样式没有影响;在 Firefox 中,设置margin:0px仅仅可以去除上下的空白,设置padding:0px后仅仅可以去掉左右缩进,还必须设置list- style:none才能去除列表编号或圆点。也就是说,在IE中仅仅设置margin:0px即可达到最终效果,而在Firefox中必须同时设置margin:0px、 padding:0px以及list-style:none三项才能达到最终效果。

8.元素水平居中问题

FF: margin:0 auto;

IE: 父级{ text-align:center; }

9.p的垂直居中问题

vertical-align:middle; 将行距增加到和整个p一样高:line-height:200px; 然后插入文字,就垂直居中了。缺点是要控制内容不要换行。

10.margin加倍的问题

设置为float的p在ie下设置的margin会加倍。这是一个ie6都存在的bug。解决方案是在这个p里面加上display:inline;

例如:

相应的css为

?


#imfloat{
float:left;
margin:5px;/*IE下理解为10px*/
display:inline;/*IE下再理解为5px*/}
Copy after login


11.IE与宽度和高度的问题

IE不认得min-这个定义,但实际上它把正常的width和height当作有min的情况来使。这样问题就大了,如果只用宽度和高度,正常的浏览器里这两个值就不会变,如果只用min-width和min-height的话,IE下面根本等于没有设置宽度和高度。

比如要设置背景图片,这个宽度是比较重要的。要解决这个问题,可以这样:

?

1

#box{ width: 80px; height: 35px;}html>body #box{ width: auto; height: auto; min-width: 80px; min-height: 35px;}

12.页面的最小宽度

如上一个问题,IE不识别min,要实现最小宽度,可用下面的方法:

?

1    #container{ min-width: 600px; width:expression(document.body.clientWidth< 600? “600px”: “auto” );}
Copy after login

13.p浮动IE文本产生3象素的bug

左边对象浮动,右边采用外补丁的左边距来定位,右边对象内的文本会离左边有3px的间距.

?






#box{ float:left; width:800px;}
#left{ float:left; width:50%;}
#right{ width:50%;}
*html #left{ margin-right:-3px; //这句是关键}
<p id=”box”>
<p id=”left”></p>
<p id=”right”></p>
</p>
Copy after login


14.IE捉迷藏的问题

当p应用复杂的时候每个栏中又有一些链接,p等这个时候容易发生捉迷藏的问题。

有些内容显示不出来,当鼠标选择这个区域是发现内容确实在页面。

解决办法:对#layout使用line-height属性或者给#layout使用固定高和宽。页面结构尽量简单。

15.float的p闭合;清除浮动;自适应高度

① 例如:

这里的NOTfloatC并不希望继续平移,而是希望往下排。(其中floatA、floatB的属性已经设置为float:left;)

这段代码在IE中毫无问题,问题出在FF。原因是NOTfloatC并非float标签,必须将float标签闭合。在

之间加上

这个p一定要注意位置,而且必须与两个具有float属性的p同级,之间不能存在嵌套关系,否则会产生异常。并且将clear这种样式定义为为如下即可:.clear{clear:both;}

②作为外部 wrapper 的 p 不要定死高度,为了让高度能自适应,要在wrapper里面加上overflow:hidden; 当包含float的box的时候,高度自适应在IE下无效,这时候应该触发IE的layout私有属性(万恶的IE啊!)用zoom:1;可以做到,这样就达到了兼容。

例如某一个wrapper如下定义:

.colwrapper{overflow:hidden; zoom:1; margin:5px auto;}

③对于排版,我们用得最多的css描述可能就是float:left.有的时候我们需要在n栏的float p后面做一个统一的背景,譬如:

<p id=”page”>
<p id=”left”></p>
<p id=”center”></p>
<p id=”right”></p>
</p>
Copy after login

比如我们要将page的背景设置成蓝色,以达到所有三栏的背景颜色是蓝色的目的,但是我们会发现随着left center right的向下拉长,而page居然保存高度不变,问题来了,原因在于page不是float属性,而我们的page由于要居中,不能设置成float,所以我们应该这样解决:

<p id=”page”>
<p id=”bg” style=”float:left;width:100%”>
<p id=”left”></p>
<p id=”center”></p>
<p id=”right”></p>
</p>
</p>
Copy after login

再嵌入一个float left而宽度是100%的p解决之。

④万能float 闭合(非常重要!)

关于 clear float 的原理可参见 [How To Clear Floats Without Structural Markup],将以下代码加入Global CSS 中,给需要闭合的p加上class=”clearfix” 即可,屡试不爽。

   
/* 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 */
Copy after login

或者这样设置:.hackbox{ display:table; //将对象作为块元素级的表格显示}

16.高度不适应

高度不适应是当内层对象的高度发生变化时外层高度不能自动进行调节,特别是当内层对象使用margin 或padding时。

例:

#box { }
#box p {margin-top: 20px;margin-bottom: 20px; text-align:center; }
<p id=”box”>
<p>p对象中的内容</p>
</p>
Copy after login

解决技巧:在P对象上下各加2个空的p对象CSS代码{height:0px;overflow:hidden;}或者为p加上border属性。

17.IE6下图片下有空隙产生

解决这个BUG的技巧有很多,可以是改变html的排版,或者设置img为display:block或者设置vertical-align属性为vertical-align:top/bottom/middle/text-bottom 都可以解决.

18.对齐文本与文本输入框

加上vertical-align:middle;

<style type=”text/css”>
<!–
input {
width:200px;
height:30px;
border:1px solid red;
vertical-align:middle;
}
–>
</style>
Copy after login

经验证,在IE下任一版本都不适用,而ff、opera、safari、chrome均OK!

19.LI中内容超过长度后以省略号显示

此技巧适用与IE、Opera、safari、chrom浏览器,FF暂不支持。

<style type=”text/css”>
<!–
li {
width:200px;
white-space:nowrap;
text-overflow:ellipsis;
-o-text-overflow:ellipsis;
overflow: hidden;
}
–>
</style>
Copy after login

20.为什么web标准中IE无法设置滚动条颜色了

解决办法是将body换成html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />
<style type=”text/css”>
<!–
html {
scrollbar-face-color:#f6f6f6;
scrollbar-highlight-color:#fff;
scrollbar-shadow-color:#eeeeee;
scrollbar-3dlight-color:#eeeeee;
scrollbar-arrow-color:#000;
scrollbar-track-color:#fff;
scrollbar-darkshadow-color:#fff;
}
–>
</style>
Copy after login

21.为什么无法定义1px左右高度的容器

IE6下这个问题是因为默认的行高造成的,解决的技巧也有很多:

例如:overflow:hidden  zoom:0.08   line-height:1px

22.怎么样才能让层显示在FLASH之上呢

解决的办法是给FLASH设置透明

<param name=”wmode” value=”transparent” />
Copy after login

23.链接(a标签)的边框与背景

a链接加边框和背景色,需设置 display: block, 同时设置 float: left 保证不换行。参照menubar, 给 a 和menubar设置高度是为了避免底边显示错位, 若不设 height, 可以在menubar中插入一个空格。

24.超链接访问过后hover样式就不出现的问题

被点击访问过的超链接样式不在具有hover和active了,很多人应该都遇到过这个问题,解决技巧是改变CSS属性的排列顺序: L-V-H-A

<style type=”text/css”>
<!–
a:link {}
a:visited {}
a:hover {}
a:active {}
–>
</style>
Copy after login

25.FORM标签

这个标签在IE中,将会自动margin一些边距,而在FF中margin则是0,因此,如果想显示一致,所以最好在css中指定margin和 padding,针对上面两个问题,我的css中一般首先都使用这样的样式ul,form{margin:0;padding:0;}。

26.属性选择器(这个不能算是兼容,是隐藏css的一个bug)

p[id]{}p[id]{}

这个对于IE6.0和IE6.0以下的版本都隐藏,FF和OPera作用.属性选择器和子选择器还是有区别的,子选择器的范围从形式来说缩小了,属性选择器的范围比较大,如p[id]中,所有p标签中有id的都是同样式的.

27.为什么FF下文本无法撑开容器的高度

标准浏览器中固定高度值的容器是不会象IE6里那样被撑开的,那我又想固定高度,又想能被撑开需要怎样设置呢?办法就是去掉height设置min-height:200px; 这里为了照顾不认识min-height的IE6 可以这样定义:

{
height:auto!important;
height:200px;
min-height:200px;
}
Copy after login

以上内容是小编给大家分享的浏览器兼容性问题大汇总 的相关叙述,希望大家喜欢。

相关推荐:

常用原生JS兼容性写法汇总_javascript技巧

The above is the detailed content of Detailed explanation of js compatibility issues. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template