IE浏览器兼容Firefox的JS脚本的代码
更新时间:2008年10月23日 15:50:47 作者:
对于经常用js的朋友,有时候一段脚本并不是两个浏览器都兼容的,下面一些对ie和firefox都兼容的一些代码,大家可以学习下。
1.window.event兼容脚本
2.屏蔽Form提交事件
3.获取事件源
4.添加事件兼容写法
5.Firefox注册innerText写法
6.长度
7.父控件下的子控件
8.XmlHttp
1.window.event兼容脚本
function getEvent(){ //获取浏览器事件,同时兼容ie和ff的写法
if(document.all) return window.event;
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0){
if((arg0.constructor==Event arg0.constructor ==MouseEvent)
(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
return arg0;
}
}
func=func.caller;
}
return null;
}
每次用事件之前Firefox都需要用getEvent()获取一下,否则就是空
2.屏蔽Form提交事件
event.returnValue=false;// for IE
evt.preventDefault();//for firefox
3.获取事件源
var source=event.srcElement //IE
var source=event.target //firefox
4.添加事件兼容写法
function addEvent(oElement,sEvent,func){
if (oElement.attachEvent){
oElement.attachEvent(sEvent,func);
}
else{
sEvent=sEvent.substring(2,sEvent.length);
oElement.addEventListener(sEvent,func,false);
}
}
用法:addEvent(window,"onload",Start);
5.Firefox注册innerText写法
//注册firefox innerText
HTMLElement.prototype.__defineGetter__("innerText",
function(){
var anyString = "";
var childS = this.childNodes;
for(var i=0; i if(childS[i].nodeType==1)
anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
else if(childS[i].nodeType==3)
anyString += childS[i].nodeValue;
}
return anyString;
}
);
HTMLElement.prototype.__defineSetter__("innerText",
function(sText){
this.textContent=sText;
}
);
6.长度:FireFox长度必须加“px”,IE无所谓
7.父控件下的子控件:IE是“children”,FireFox是“childNodes”
8.XmlHttp
在IE中,XmlHttp.send(content)方法的content可以为空,而firefox则不能为空,应该用send(" "),否则会出现411错误
2.屏蔽Form提交事件
3.获取事件源
4.添加事件兼容写法
5.Firefox注册innerText写法
6.长度
7.父控件下的子控件
8.XmlHttp
1.window.event兼容脚本
function getEvent(){ //获取浏览器事件,同时兼容ie和ff的写法
if(document.all) return window.event;
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0){
if((arg0.constructor==Event arg0.constructor ==MouseEvent)
(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){
return arg0;
}
}
func=func.caller;
}
return null;
}
每次用事件之前Firefox都需要用getEvent()获取一下,否则就是空
2.屏蔽Form提交事件
event.returnValue=false;// for IE
evt.preventDefault();//for firefox
3.获取事件源
var source=event.srcElement //IE
var source=event.target //firefox
4.添加事件兼容写法
function addEvent(oElement,sEvent,func){
if (oElement.attachEvent){
oElement.attachEvent(sEvent,func);
}
else{
sEvent=sEvent.substring(2,sEvent.length);
oElement.addEventListener(sEvent,func,false);
}
}
用法:addEvent(window,"onload",Start);
5.Firefox注册innerText写法
//注册firefox innerText
HTMLElement.prototype.__defineGetter__("innerText",
function(){
var anyString = "";
var childS = this.childNodes;
for(var i=0; i if(childS[i].nodeType==1)
anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
else if(childS[i].nodeType==3)
anyString += childS[i].nodeValue;
}
return anyString;
}
);
HTMLElement.prototype.__defineSetter__("innerText",
function(sText){
this.textContent=sText;
}
);
6.长度:FireFox长度必须加“px”,IE无所谓
7.父控件下的子控件:IE是“children”,FireFox是“childNodes”
8.XmlHttp
在IE中,XmlHttp.send(content)方法的content可以为空,而firefox则不能为空,应该用send(" "),否则会出现411错误
相关文章
echarts报错Cannot read properties of null (reading ‘getA
最近在开发Echarts忽然遇到了个问题,这篇文章主要给大家介绍了关于echarts报错Cannot read properties of null (reading ‘getAttribute‘)的解决方法,需要的朋友可以参考下2023-01-01
使用getBoundingClientRect方法实现简洁的sticky组件的方法
本文介绍这种组件的实现思路,并提供一个同时支持将sticky元素固定在顶部或底部的具体实现,由于这种组件在网站中非常常见,所以有必要掌握它的实现方式,以便在有需要的时候基于它的思路写出功能更多的组件出来2016-03-03
TypeScript中使用getElementXXX()的示例代码
这篇文章主要介绍了TypeScript中使用getElementXXX()的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-09-09


最新评论