您的位置:首页 > Web前端 > JavaScript

JS浏览器兼容性问题

2015-12-09 16:05 555 查看
一下以IE代替Internet Explorer,以MF/FF代替Mozzila Firefox。

//window.event

IE:有window.event对象

FF:没有window.event对象。可以通过给函数的参数传递event对象。如onmousemove=doMouseMove(event)

//鼠标当前坐标

IE:event.x和event.y

FF:event.pageX和event.pageY

通用:两者都可以用event.clientX和event.clientY属性,所以建议使用event.clientX和event.clientY。

//event.srcElement问题

说明:IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event有target属性,但是没有srcElement属性。

解决方法:使用obj(obj=event.srcElement?event.srcElement:event.target)来代替IE下的event.srcElement或者Firefox下的event。target。请同时注意event的兼容性问题。

//event.toElement问题

问题:

IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event有target属性,但是没有srcElement属性。

解决方法:

var target=e.relatedTarget||e.toElement.

//标签的x和y的坐标位置:style.posLeft和style.posTop

IE:有。

FF:没有。

通用:obj.offetLeft和obj.offsetTop.

//窗体的高度和宽度

IE:document.body.offsetWidth和document.body.offsetHeight。注意:此时页面一定要有body标签。

FF:window.innerWidth和window.innerHeight,以及document.documentElement.clientWidth和document.documentElement.clientHeight。

同样:document.body.clientWidth和document.body.clientHeight。

//添加事件

IE:element.attachEvent('onclick',function);

FF:element.addEventListener(‘click’,function,true)。

通用:element.onclick=fucntion。虽说都可以使用onclick事件,但是onclick和上面两种方法的效果是不一样的,onclick只有执行一个过程。而attachEvent和addEventListener执行的是一个过程列表,也就是说多个过程。例如:element.attachEvent('onclick',func1);element.attach('onclick',func2);这样func1和func2都会被执行。

//标签的自定义属性

IE:如果给标签div1定义了一个属性value,可以div1.value和div1["value"]取得该值。

FF:不能用div1.value和div1["value"]取。

通用:div1.getAttribute("value");

//document.form.item问题

IE:现有问题 现有代码中存在许多document.formName.item('itemName')这样的语句,不能在MF下运行。

FF/IE:document.formName.elements['elementName'];

//集合/数组类对象问题

(1)现有问题:现有代码中许多集合类对象取用使用(),IE能接受,MF不能。

(2)解决方法:改变[]作为下标运算。如:document.forms("formName")改为document.forms("formName")。

又如:document.getElementByTagName("inputName")(1)改为document.getElementByName("inputName")[1]。

//HTML对象的id作为对象名的问题

(1)现有问题

在IE中,HTML对象的ID可以做为document的下属对象变量名直接使用。在MF中不能。

(2)解决方法

用getElementById('idName')代替idName作为对象变量使用。

//用idName字符串取得对象问题

(1)现有问题

在I
d5be
E中,利用eval(idName)可以取得id为idName的HTML对象,在MF中不能。

(2)解决方法

用getElementById(idName)代替eval(idName)。

//变量名与某HTML对象id相同问题

//变量名与某 HTML 对象 id 相同的问题  

(1)现有问题  

    在 MF 中,因为对象 id 不作为 HTML 对象的名称,所以可以使用与 HTML 对象 id 相同的变量名,IE 中不能。  

(2)解决方法  

    在声明变量时,一律加上 var ,以避免歧义,这样在 IE 中亦可正常运行。  

    此外,最好不要取与 HTML 对象 id 相同的变量名,以减少错误。  

  

//document.getElementsByName() 和 document.all[name] 的问题  

现有问题:在 IE 中,getElementsByName()、document.all[name] 均不能用来取得 div 元素(是否还有其它不能取的元素还不知道)。  

//document.all  

Firefox可以兼容document.all, 但会生成一条警告。可以用getElementById("*") 或者 getElementByTagName("*")来代替  

不过对于document.all.length等属性,则完全不兼容  

  

//input.type属性问题   

说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写  

  

//window.location.href问题   

说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location  

解决方法:使用window.location来代替window.location.href  

  

//模态和非模态窗口问题   

说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能  

解决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。   

如果需要将子窗口中的参数传递回父窗口,可以在子窗口中使用window.opener来访问父窗口. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";   

  

//frame问题   

以下面的frame为例:   

<frame src="xxx.html" mce_src="xxx.html" id="frameId" name="frameName" />   

(1)访问frame对象:   

IE:使用window.frameId或者window.frameName来访问这个frame对象. frameId和frameName可以同名。   

FF:只能使用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来访问父窗口。例如:window.parent.document.form1.filename.value="Aqing";   

  

//body问题   

Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在  

  

//事件委托方法   

IE:document.body.onload = inject; //Function inject()在这之前已被实现   

FF:document.body.onload = inject();   

  

//firefox与IE的父元素(parentElement)的区别   

IE:obj.parentElement   

FF:obj.parentNode   

解决方法: 因为FF与IE都支持DOM,因此使用obj.parentNode是不错选择  

  

//innerText在IE中能正常工作,但是innerText在FireFox中却不行. 需用textContent  

  

//FireFox中设置HTML标签的style时,所有位置性和字体尺寸的值必须后跟px。这个ie也是支持的  

  

//父节点、子节点和删除节点   

IE:parentElement、parement.children,element.romoveNode(true)。   

FF:parentNode、parentNode.childNodes,node.parentNode.removeChild(node)。   

  

//对select的options集合操作   

枚举元素除了[]外,SelectName.options.item()也是可以的, 另外SelectName.options.length, SelectName.options.add/remove都可以在两种浏览器上使用。  

注意在add后赋值元素,否则会失败  

动态删除select中的所有options:   

       document.getElementById("ddlResourceType").options.length=0;   

动态删除select中的某一项option:   

       document.getElementById("ddlResourceType").options.remove(indx);    

动态添加select中的项option:   

       document.getElementById("ddlResourceType").options.add(new Option(text,value));   

IE FF 动态删除通用方法:  

document.getElementById("ddlResourceType").options[indx] = null;  

  

//捕获事件   

问题:   

FF没有setCapture()、releaseCapture()方法   

解决方法:   

IE:   

obj.setCapture();   

obj.releaseCapture();   

FF:   

window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);   

window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);   

if (!window.captureEvents) {   

       o.setCapture();   

}else {   

       window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);   

}   

if (!window.captureEvents) {   

       o.releaseCapture();   

}else {   

       window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);   

}   

  

  

//禁止选取网页内容   

问题:   

FF需要用CSS禁止,IE用JS禁止   

解决方法:   

IE: obj.onselectstart = function() {return false;}   

FF: -moz-user-select:none;   

  

  

//画图   

IE:VML。   

FF:SVG。   

  

//CSS:透明   

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

FF:opacity:0.6。   

  

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

  

//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;。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: