`
aihhd2008
  • 浏览: 24406 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
最近访客 更多访客>>
社区版块
存档分类
最新评论

JavaScript在IE和Firefox下的兼容性问题

    博客分类:
  • web
阅读更多
(一)
问题1:获取一个元素对象的引用,在IE下,可直接使用该元素对象的id名;而在FireFox下,只能使用getElementById(idName)方法。
解决方法:统一用getElementById(idName)。 
问题2:获取表单元素的引用,在IE下,可通过form.item('username')这种形式;而在FireFox下,只能通过form.elements['username']。(注:var form = document.getElementById("form");)
解决方法:统一用form.elements['username']这种形式。
问题3:访问集合对象成员时,在IE下,可通过
Js代码
1. var elements = form.elements; var firstElement = elements(0);  

 而在Firefox下,只能通过
Java代码
1. var elements = form.elements; var firstElement = elements[0];  

 解决方法:统一用方括号加下标索引来访问。
问题4:读取自定义属性,在IE下,可像操作对象属性一样直接访问;而在FireFox下,只能通过getAttribute(attrName)方式访问。
解决方法:统一用getAttribute(attrName)方式访问。
问题5:常量的定义,在FireFox下能通过const关键词定义,如const PI = 3.14; 而在IE下却报错。
解决方法:统一用var定义,如var PI = 3.14; 区别常量和变量可用大小写字母来区别。
问题6:input元素的type属性,在FireFox下可修改其type的值;如:
Js代码
1. var input = document.getElementById("button1");  
	2. input.type = "text";  //将按钮变成输入框  

 但在IE下却不能这样使用。
解决方法:无
问题7:模式窗口window.showModalDialog(...)和非模式窗口window.showModelessDialog(...),只能在IE下运行,而在Firefox下却用不了。
解决方法:统一用window.open(...)代替。
问题8:frame操作问题,在IE中,可通过window.frameId或者window.frameName即可获得对frame页面window对象的引用; 而在FireFox下,只能通过window.frameName来获得。如下:
Html代码 
	1. <script type="text/javascript">  
	2.      //只有IE支持  
	3.     //window.frameId.location = "http://www.iteye.com";  
	4.     //IE,FireFox都支持  
	5.     window.frameName.location = "http://www.iteye.com";  
	6.     //IE,FireFox都支持  
	7.     //document.getElementById("frameId").contentWindow.location = "http://www.iteye.com";  
	8. </script>  
	9.   
	10. <iframe id="frameId" name="frameName" src="about:blank" width="750" height="500" />  

 
问题9:读取和设置一个元素内的文本,在IE下,可用innerText属性;而在fireFox下,可用textContent属性,两者效果一样。
解决方法:让FireFox也支持innerText属性,代码如下:
问题10:对父元素的引用,在IE下,可用parentElement和parentNode;而在FireFox下,只能使用parentNode。
解决方法:统一用parentNode。


(二)

1、发现IE下input标签的id属性默认和name属性相同,而Firefox必须明确写出id属性的名称否则不能使用id属性。
如:
<input type="text" name="username" value=""> 
在IE下如下代码可以执行而在Firefox下却不可以: 
<script> 
alert(document.getElementById("username").value); 
</script> 
必须改为如下代码才可以: 
<input type="text" name="username" id="username" value=""> 

以下为转载:
1. document.formName.item("itemName") 问题
说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements["elementName"];
Firefox下,只能使用document.formName.elements["elementName"].
解决方法:统一使用document.formName.elements["elementName"].
2.集合类对象问题
说明:IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象.
解决方法:统一使用[]获取集合类对象.
3.自定义属性问题
说明:IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性.
解决方法:统一通过getAttribute()获取自定义属性.
4.eval("idName")问题
说明:IE下,,可以使用eval("idName")或getElementById("idName")来取得id为idName的HTML对象;Firefox下只能使用getElementById("idName")来取得id为idName的HTML对象.
解决方法:统一用getElementById("idName")来取得id为idName的HTML对象.
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属性问题
说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写.
8.window.event问题
说明:window.event只能在IE下运行,而不能在Firefox下运行,这是因为Firefox的event只能在事件发生的现场使用. Firefox必须从源处加入event作参数传递。Ie忽略该参数,用window.event来读取该event。
解决方法:
IE&Firefox: 
Submitted(event)"/> … 
<script language="javascript"> 
function Submitted(evt) { 
evt=evt?evt:(window.event?window.event:null); 
} 
</script> 
window.open("b.html","","modal=yes,width=500,height=500,resizable=no,scrollbars=no"); 

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.
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.location或window.location.href;Firefox1.5.x下,只能使用window.location.
解决方法:使用window.location来代替window.location.href.
12.模态和非模态窗口问题
说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能.
解决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。
如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";
13.frame问题
以下面的frame为例:
<frame src="xxx.html" id="frameId" name="frameName" />
(1)访问frame对象:
IE:使用window.frameId或者window.frameName来访问这个frame对象. frameId和frameName可以同名。
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中的参数传回父窗口(注意不是opener,而是parent frame),可以在frme中使用parent来访问父窗口。例如:parent.document.form1.filename.value="Aqing";
14.body问题
Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.
15. 事件委托方法
IE:document.body.onload = inject; //Function inject()在这之前已被实现
Firefox:document.body.onload = inject();
16. firefox与IE的父元素(parentElement)的区别
IE:obj.parentElement
firefox:obj.parentNode
解决方法: 因为firefox与IE都支持DOM,因此使用obj.parentNode是不错选择.
17.cursor:hand VS cursor:pointer
firefox不支持hand,但ie支持pointer
解决方法: 统一使用pointer
18.innerText在IE中能正常工作,但是innerText在FireFox中却不行. 需用textContent。
解决方法:
if(navigator.appName.indexOf("Explorer") > -1){ 
document.getElementById('element').innerText = "my text"; 
} else{ 
document.getElementById('element').textContent = "my text"; 
} 

19. FireFox中设置HTML标签的style时,所有位置性和字体尺寸的值必须后跟px。这个ie也是支持的。
20. 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)
;
21. padding 问题
padding 5px 4px 3px 1px FireFox无法解释简写,
必须改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;
22. 消除ul、ol等列表的缩进时
样式应写成:list-style:none;margin:0px;padding:0px;
其中margin属性对IE有效,padding属性对FireFox有效
23. CSS透明
IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。 
FF:opacity:0.6。

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双线凹凸边框
IE:border: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. 对select的options集合操作
枚举元素除了[]外,selectName.options.item()也是可以的, 另外selectName.options.length, selectName.options.add/remove都可以在两种浏览器上使用。注意在add后赋值元素,否则会失败(本人试验如此)。
27. XMLHTTP的区别
//mf 
if (window.XMLHttpRequest) //mf 
{ 
xmlhttp=new XMLHttpRequest() 
xmlhttp. 
xmlhttp.open("GET",url,true) 
xmlhttp.send(null) 
} 
//ie 
else if (window.ActiveXObject) // code for IE 
{ 
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP") 
if (xmlhttp) 
{ 
xmlhttp. 
xmlhttp.open("GET",url,true) 
xmlhttp.send() 
} 
} 
} 

28. innerHTML的区别
Firefox不支持innerHTML, 解决办法可以如下
rng = document.createRange(); 
el = document.getElementById(elementid); 
rng.setStartBefore(el); 
htmlFrag = rng.createContextualFragment(content); 
while (el.hasChildNodes()) //清除原有内容,加入新内容 
el.removeChild(el.lastChild); 
el.appendChild(htmlFrag); 

29. img的src刷新问题
在IE 下可以用<img id="pic" onclick= "this.src= 'aa.php'" src="aa.php" style="cursor: pointer"/> 可以刷新图片,但在FireFox下不行。主要是缓存问题,在地址后面加个随机数就解决了。编辑onclick事件代码如下: "this.src=this.src+'?'+Math.random()"

分享到:
评论
4 楼 光影门徒 2010-03-04  
总结的很不错,谢谢了
3 楼 zy2419 2010-03-04  
总结的很好啊
2 楼 ccyingfu 2010-03-03  
Firefox支持innerHTML呀....是哪个版本不支持?
1 楼 JasonChi 2010-03-02  
SF 学习

相关推荐

Global site tag (gtag.js) - Google Analytics