ITeye博客

IE与FF兼容在JS方面要注意的一些问题

as1001001 · 2010-10-13 09:09 · 技术转载 · Web前端 · Markdown

关于IE、Firefox、Opera页面呈现异同 写脚本很痛苦,对于多浏览器的兼容性,需要注意。

1.document.formName.item("itemName") 问题 说明:IE下,可以使用document.formName.item("itemName")或document.formName.elements ;Firefox下,只能使用document.formName.elements. 解决方法:统一使用document.formName.elements. 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只能在事件发生的现场使用. 解决方法: IE: ...

IE&Firefox: ...

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下,even对象有srcElement属性,但是没有target属性;Firefox下,even对象有target属性,但是没有srcElement属性. 解决方法:使用obj(obj = event.srcElement ? event.srcElement : event.target;)来代替IE下的event.srcElement或者Firefox下的event.target. 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为例:

  1. 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中却不行. 解决方法: if(navigator.appName.indexOf("Explorer") > -1){ document.getElementById('element').innerText = "my text"; } else{ document.getElementById('element').textContent = "my text"; }

  2. FireFox中类似 obj.style.height = imgObj.height 的语句无效 解决方法: obj.style.height = imgObj.height + 'px';

  3. ie,firefox以及其它浏览器对于 table 标签的操作都各不相同,在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChile方法也不管用。 解决方法: //向table追加一个空行: var row = otable.insertRow(-1); var cell = document.createElement("td"); cell.innerHTML = " "; cell.className = "XXXX"; row.appendChild(cell);

  4. padding 问题 padding 5px 4px 3px 1px FireFox无法解释简写, 必须改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;

  5. 消除ul、ol等列表的缩进时 样式应写成:list-style:none;margin:0px;padding:0px; 其中margin属性对IE有效,padding属性对FireFox有效

  6. CSS透明 IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。 FF:opacity:0.6。

  7. 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;。

  8. 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.IIE对比FF:空<li></li>解析问题 FF:空<li></li>标签所带的css样式是不被解释的。 IE:空<li></li>Css是一样会被解析的。 27.ff下的click事件替代 var evt = document.createEvent('HTMLEvents'); evt.initEvent('click',true,true);

element = document.getElementById('id'); element.dispatchEvent(evt); 28.ff下option添加元素 document.getElementById("id").options.add(optionObj); 29.在IE和FF下新建option项 function createOption(s, txt, v) { var eItem = document.createElement ("OPTION"); s.options.add (eItem); if(navigator.appName.indexOf("Explorer") > -1) { eItem.innerText = txt; } else { eItem.textContent = txt; } eItem.value = v; } 30.火狐下onkeyup的处理方法 obj.onkeyup = new Function ("showList_onkeyup(this);");

function showList_onkeyup(obj, e) { var e = arguments.callee.caller.arguments ||event; var currKey = e.keyCode || e.which || e.charCode; //Esc 27; Enter 13 if(currKey == 27 || currKey == 13) hiddenTips(); }

详细出处参考:http://www.jb51.net/article/19794.htm

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。