JavaScript在IE与Firfox中区别.docx
《JavaScript在IE与Firfox中区别.docx》由会员分享,可在线阅读,更多相关《JavaScript在IE与Firfox中区别.docx(23页珍藏版)》请在冰豆网上搜索。
JavaScript在IE与Firfox中区别
1.CSS“float”属性
获取给定对象的特定CSS属性的基本语法是object.style属性,而且有连字符的属性要用骆驼命名法来代替。
例如,获取一个ID为“header”的div的background-color属性,我们要用如下语法:
document.getElementById("header").style.borderBottom="1pxsolid#ccc";
但是由于“float”是JavaScript的保留词,我们就无法使用object.style.float来获取“float”属性了。
一下是我们在两种浏览器中的使用的方法:
IE语法:
document.getElementById("header").style.styleFloat="left";
Firefox语法:
document.getElementById("header").style.cssFloat="left";
2.元素的计算样式
通过使用上述的object.style.property,JavaScript可以很容易的获取和修改对象的设定CSS样式。
但是这一语法的局限在于,它只能取得内联在HTML里的样式,或者直接使用JavaScript设定的样式。
style对象不能获取使用外部样式表设定的样式。
为了获取对象的”计算样式”,我们使用以下代码:
IE语法:
varmyObject=document.getElementById("header");
varmyStyle=myObject.currentStyle.backgroundColor;
Firefox语法:
varmyObject=document.getElementById("header");
varmyComputedStyle=document.defaultView.getComputedStyle(myObject,null);
varmyStyle=myComputedStyle.backgroundColor;
3.获取元素的”class”属性
类似于”float”属性的情况,这两种浏览器使用不同的JavaScript方法来获取这个属性。
IE语法:
varmyObject=document.getElementById("header");
varmyAttribute=myObject.getAttribute("className");
Firefox语法:
varmyObject=document.getElementById("header");
varmyAttribute=myObject.getAttribute("class");
4.获取label标签的“for”属性
和3一样,使用JavaScript获取label的“for”属性也有不同语法。
IE语法:
varmyObject=document.getElementById("myLabel");
varmyAttribute=myObject.getAttribute("htmlFor");
Firefox语法:
varmyObject=document.getElementById("myLabel");
varmyAttribute=myObject.getAttribute("for");
对于setAtrribute方法来说也是同样的语法。
5.获取光标位置
获取元素的光标位置比较少见,如果需要这么做,IE和Firefox的语法也是不同的。
这个示例代码是相当基础的,一般用作许多复杂事件处理的一部分,这里仅用来描述差异。
需要注意的是,IE中的结果和Firefox中是不同的,因此这个方法有些问题。
通常,这个差异可以通过获取“滚动位置”来补偿-但那是另外一篇文章的课题了。
IE语法:
varmyCursorPosition=[0,0];
myCursorPosition[0]=event.clientX;
myCursorPosition[1]=event.clientY;
Firefox语法:
varmyCursorPosition=[0,0];
myCursorPosition[0]=event.pageX;
myCursorPosition[1]=event.pageY;
6.获取视窗或浏览器窗口的尺寸
有时需要找出浏览器的有效窗口空间的尺寸,一般成为”视窗”。
IE语法:
varmyBrowserSize=[0,0];
myBrowserSize[0]=document.documentElement.clientWidth;
myBrowserSize[1]=document.documentElement.clientHeight;
Firefox语法:
varmyBrowserSize=[0,0];
myBrowserSize[0]=window.innerWidth;
myBrowserSize[1]=window.innerHeight;
7.Alpha透明
嗯,这其实不是JavaScript的语法项目-alpha透明是通过CSS来设置的。
但是当对象通过JavaScript设置为淡入淡出时,这就需要通过获取CSS的alpha设定来实现,一般是在循环内部。
要通过以下JavaScript来改变CSS代码:
IE语法:
#myElement{
filter:
alpha(opacity=50);
}
Firefox语法:
#myElement{
opacity:
0.5;
}
要使用JavaScript获取这些值,需要使用style对象:
IE语法:
varmyObject=document.getElementById("myElement");
myObject.style.filter="alpha(opacity=80)";
Firefox语法:
varmyObject=document.getElementById("myElement");
myObject.style.opacity="0.5";
8.document.formName.item(itemName)问题
说明:
IE下,可以使用document.formName.item(itemName)或document.formName.elements[elementName];Firefox下,只能使用document.formName.elements[elementName].
解决方法:
统一使用document.formName.elements[elementName].
9.集合类对象问题
说明:
IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象.
解决方法:
统一使用[]获取集合类对象.
10.自定义属性问题
说明:
IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性.
解决方法:
统一通过getAttribute()获取自定义属性.
11.eval(idName)问题
说明:
IE下,,可以使用eval(idName)或getElementById(idName)来取得id为idName的HTML对象;Firefox下只能使用getElementById(idName)来取得id为idName的HTML对象.
解决方法:
统一用getElementById(idName)来取得id为idName的HTML对象.
12.变量名与某HTML对象ID相同的问题
说明:
IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox下,可以使用与HTML对象ID相同的变量名;IE下则不能。
解决方法:
使用document.getElementById(idName)代替document.idName.最好不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义.
13.const问题
说明:
Firefox下,可以使用const关键字或var关键字来定义常量;IE下,只能使用var关键字来定义常量.
解决方法:
统一使用var关键字来定义常量.
14.input.type属性问题
说明:
IE下input.type属性为只读;但是Firefox下input.type属性为读写.
15.window.event问题
说明:
window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用.
解决方法:
IE:
gotoSubmit8_1()"/>
...
functiongotoSubmit8_1(){
...
alert(window.event);//usewindow.event
...
}
IE&Firefox:
gotoSubmit8_2(event)"/>
...
functiongotoSubmit8_2(evt){
...
evt=evt?
evt:
(window.event?
window.event:
null);
alert(evt);//useevt
...
}
9.event.x与event.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.
16.event.srcElement问题
说明:
IE下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性.
解决方法:
使用obj(obj=event.srcElement?
event.srcElement:
event.target;)来代替IE下的event.srcElement或者Firefox下的event.target.
17.window.location.href问题
说明:
IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location.
解决方法:
使用window.location来代替window.location.href.
18.模态和非模态窗口问题
说明:
IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能.
解决方法:
直接使用window.open(pageURL,name,parameters)方式打开新窗口。
如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口.例如:
varparWin=window.opener;parWin.document.getElementById(Aqing).value=Aqing;
19.frame问题
以下面的frame为例:
(1)访问frame对象:
IE:
使用window.frameId或者window.frameName来访问这个frame对象.
Firefox:
只能使用window.frameName来访问这个frame对象.
另外,在IE和Firefox中都可以使用window.document.getElementById(frameId)来访问这个frame对象.
(2)切换frame内容:
在IE和Firefox中都可以使用window.document.getElementById(testFrame).src=xxx.html或window.frameName.location=xxx.html来切换frame的内容.
如果需要将frame中的参数传回父窗口,可以在frme中使用parent来访问父窗口。
例如:
parent.document.form1.filename.value=Aqing;
(3)访问frame页面function
VarpageLabel=document.getElementById(‘pageLabel’);
IE:
pageLabel.openLabel或pageLabel.contentWindow.openLabel
FF:
pageLabel.contentWindow.openLabel
20.body问题
Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.
Firefox:
document.body.onclick=function(evt){
evt=evt||window.event;
alert(evt);
}
IE&Firefox:
document.body.onclick=function(evt){
evt=evt||window.event;
alert(evt);
}
21.事件委托方法
IE:
document.body.onload=inject;//Functioninject()在这之前已被实现
Firefox:
document.body.onload=inject();
有人说标准是:
document.body.onload=newFunction('inject()');
22.firefox与IE(parentElement)的父元素的区别
IE:
obj.parentElement
firefox:
obj.parentNode
解决方法:
因为firefox与IE都支持DOM,因此使用obj.parentNode是不错选择.
23.cursor:
handVScursor:
pointer
firefox不支持hand,但ie支持pointer
解决方法:
统一使用pointer
18.innerText在IE中能正常工作,但是innerText在FireFox中却不行.
解决方法:
if(navigator.appName.indexOf(Explorer)>-1){
document.getElementById('element').innerText=mytext;
}else{
document.getElementById('element').textContent=mytext;
}
24.FireFox中类似obj.style.height=imgObj.height的语句无效
解决方法:
obj.style.height=imgObj.height+'px';
25.ie,firefox以及其它浏览器对于table标签的操作都各不相同
在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChile方法也不管用。
解决方法:
//向table追加一个空行:
varrow=otable.insertRow(-1);
varcell=document.createElement(td);
cell.innerHTML=;
cell.className=XXXX;
row.appendChild(cell);
26.padding问题
padding5px4px3px1pxFireFox无法解释简写,必须改成padding-top:
5px;padding-right:
4px;padding-bottom:
3px;padding-left:
1px;
27.消除ul、ol等列表的缩进时
样式应写成:
list-style:
none;margin:
0px;padding:
0px;
其中margin属性对IE有效,padding属性对FireFox有效
28.CSS透明
IE:
filter:
progid:
DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
FF:
opacity:
0.6。
29.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;。
30.CSS双线凹凸边框
IE:
border:
2pxoutset;。
FF:
-moz-border-top-colors:
#d4d0c8white;-moz-border-left-colors:
#d4d0c8white;-moz-border-right-colors:
#404040#808080;-moz-border-bottom-colors:
#404040#808080;
31. body 对象
MF的body在body标签没有被浏览器完全读入之前就存在,而IE则必须在body完全被读入之后才存在
32. url encoding
在js中如果书写url就直接写&不要写&例如var url = 'xx.jsp?
objectName=xx&objectEvent=xxx';
frm.action = url那么很有可能url不会被正常显示以至于参数没有正确的传到服务器
一般会服务器报错参数没有找到
当然如果是在tpl中例外,因为tpl中符合xml规范,要求&书写为&
一般MF无法识别js中的&
33. nodeName 和 tagName 问题
(1)现有问题:
在MF中,所有节点均有 nodeName 值,但 textNode 没有 tagName 值。
在 IE 中,nodeName 的使用好象
有问题(具体情况没有测试,但我的IE已经死了好几次)。
(2)解决方法:
使用 tagName,但应检测其是否为空。
34. 元素属性
IE下 input.type属性为只读,但是MF下可以修改
35. document.getElementsByName() 和 document.all[name] 的问题
(1)现有问题:
在 IE 中,getElementsByName()、document.all[name] 均不能用来取得 div 元素(是否还有其它不能取的元素还不知道)。
1,document.getElementById替代document.all(ie适用)
2,集合[]替代()(ie适用)
3,target替代srcElement;parentNode替代parentElement(parentNodeie适用)
4,node.parentNode.removeChild(node)替代removeNode(this)(ie适用)
5,有空白文本节点
6,无outerHTML属性
7,事件局部变量e替代事件全局变量event
8,e.button键值有别于event.button,只有3个键值而无组合键值
9,无ondrag事件
10,DOMMouseScroll替代onmousewheel;-e.detail替代event.wheelDelta
11,addEventListener替代attachEvent;removeEventListener替代detachEvent
12,e.preventDefault()替代event.returnValue=false;e.stopPropagation()替代event.cancelBubble=true
13,style.top、style.left等严格检查"px"单位(加"px"ie适用)
14,style="-moz-opacity:
0.9"替代style="filter:
alpha(opacity=90)";无其它filter
15,style.cursor="pointer"替代style.cursor="hand"(ie适用)
16,title替代alt(ie适用)
17,状态栏默认不可修改,需调整ff设置
18,内置绘图功能以canvas或者SVG替代vml
19,代码出错时经常不报错(想来也是ff的无奈之举吧,如果每个ie独有的表达方式换在它里面都报错的话,怕是报都报不过来吧)
20,对缓存的清理非常不好
注:
标明“ie适用”者为通用性建议写法,未标明者在ie里不适用。
以下所有IE指IE6.0
验证是否是IE浏览器(来之于googlejs)
varagt=navigator.userAgent.toLowerCase();
varis_ie=(agt.indexOf("msie")!
=-1&&document.all);
正式开始
事件委托方法
IE
document.body.onload=inject;//Functioninject()在这之前已被实现
firefox
document.body.onload=inject();
有人说标准是:
document.body.onload=newFunction('inject()');
在firefox无法取得event.srcElement
通过其他方式传递对象
if(i