javascript firefox兼容ie的dom方法脚本


Posted in Javascript onMay 18, 2008

if(!document.all){
//zzcv的ff ie兼容脚本
/*脚本没有解决的问题及处理:

2.IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象. 
解决方法:统一使用[]获取集合类对象. 
3.IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性. 
解决方法:统一通过getAttribute()获取自定义属性. 
4.IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.
5.Firefox下,可以使用与HTML对象ID相同的变量名;IE下则不能。
解决方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义. 
6.IE下input.type属性为只读;但是Firefox下input.type属性为读写. 
8.IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能
9.Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在
10.
*/
//文档兼容
HTMLDocument.prototype.__defineGetter__("all",function(){
    return this.getElementsByName("*");});

HTMLFormElement.constructor.prototype.item=function(s){
    return this.elements[s];};

HTMLCollection.prototype.item=function(s){
    return this[s];};

//事件兼容
window.constructor.prototype.__defineGetter__("event",function(){
    for(var o=arguments.callee.caller,e=null;o!=null;o=o.caller){
        e=o.arguments[0];
        if(e&&(e instanceof Event))
            return e;}
    return null;});

window.constructor.prototype.attachEvent=HTMLDocument.prototype.attachEvent=HTMLElement.prototype.attachEvent=function(e,f){
    this.addEventListener(e.replace(/^on/i,""),f,false);};

window.constructor.prototype.detachEvent=HTMLDocument.prototype.detachEvent=HTMLElement.prototype.detachEvent=function(e,f){
    this.removeEventListener(e.replace(/^on/i,""),f,false);};

with(window.Event.constructor.prototype){
    __defineGetter__("srcElement",function(){
        return this.target;});

    __defineSetter__("returnValue",function(b){
        if(!b)this.preventDefault();});

    __defineSetter__("cancelBubble",function(b){
        if(b)this.stopPropagation();});

    __defineGetter__("fromElement",function(){
        var o=(this.type=="mouseover"&&this.relatedTarget)||(this.type=="mouseout"&&this.target)||null;
        if(o)
            while(o.nodeType!=1)
                o=o.parentNode;
        return o;});

    __defineGetter__("toElement",function(){
        var o=(this.type=="mouseover"&&this.target)||(this.type=="mouseout"&&this.relatedTarget)||null;
        if(o)
            while(o.nodeType!=1)
                o=o.parentNode;
        return o;});

    __defineGetter__("x",function(){
        return this.pageX;});

    __defineGetter__("y",function(){
        return this.pageY;});

    __defineGetter__("offsetX",function(){
        return this.layerX;});

    __defineGetter__("offsetY",function(){
        return this.layerY;});
}
//节点操作兼容
with(window.Node.prototype){
    replaceNode=function(o){
        this.parentNode.replaceChild(o,this);}

    removeNode=function(b){
        if(b)
            return this.parentNode.removeChild(this);
        var range=document.createRange();
        range.selectNodeContents(this);
        return this.parentNode.replaceChild(range.extractContents(),this);}

    swapNode=function(o){
        return this.parentNode.replaceChild(o.parentNode.replaceChild(this,o),this);}

    contains=function(o){
        return o?((o==this)?true:arguments.callee(o.parentNode)):false;}
}
//HTML元素兼容
with(window.HTMLElement.prototype){
    __defineGetter__("parentElement",function(){
        return (this.parentNode==this.ownerDocument)?null:this.parentNode;});

    __defineGetter__("children",function(){
        var c=[];
        for(var i=0,cs=this.childNodes;i<cs.length;i++){
            if(cs[i].nodeType==1)
                c.push(cs[i]);}
        return c;});

    __defineGetter__("canHaveChildren",function(){
        return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/i.test(this.tagName);});

    __defineSetter__("outerHTML",function(s){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        void this.parentNode.replaceChild(r.createContextualFragment(s),this);
        return s;});
    __defineGetter__("outerHTML",function(){
        var as=this.attributes;
        var str="<"+this.tagName;
        for(var i=0,al=as.length;i<al;i++){
            if(as[i].specified)
                str+=" "+as[i].name+"=""+as[i].value+""";}
        return this.canHaveChildren?str+">":str+">"+this.innerHTML+"</"+this.tagName+">";});

    __defineSetter__("innerText",function(s){
        return this.innerHTML=document.createTextNode(s);});
    __defineGetter__("innerText",function(){
        var r=this.ownerDocument.createRange();
        r.selectNodeContents(this);
        return r.toString();});

    __defineSetter__("outerText",function(s){
        void this.parentNode.replaceChild(document.createTextNode(s),this);
        return s});
    __defineGetter__("outerText",function(){
        var r=this.ownerDocument.createRange();
        r.selectNodeContents(this);
        return r.toString();});

    insertAdjacentElement=function(s,o){
        return (s=="beforeBegin"&&this.parentNode.insertBefore(o,this))||(s=="afterBegin"&&this.insertBefore(o,this.firstChild))||(s=="beforeEnd"&&this.appendChild(o))||(s=="afterEnd"&&((this.nextSibling)&&this.parentNode.insertBefore(o,this.nextSibling)||this.parentNode.appendChild(o)))||null;}

    insertAdjacentHTML=function(s,h){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        this.insertAdjacentElement(s,r.createContextualFragment(h));}

    insertAdjacentText=function(s,t){
        this.insertAdjacentElement(s,document.createTextNode(t));}
}
//XMLDOM兼容
window.ActiveXObject=function(s){
    switch(s){
        case "XMLDom":
        document.implementation.createDocument.call(this,"text/xml","", null);
        //domDoc = document.implementation.createDocument("text/xml","", null);
        break;
        }
    }

XMLDocument.prototype.LoadXML=function(s){
    for(var i=0,cs=this.childNodes,cl=childNodes.length;i<cl;i++)
        this.removeChild(cs[i]);
    this.appendChild(this.importNode((new DOMParser()).parseFromString(s,"text/xml").documentElement,true));}

XMLDocument.prototype.selectSingleNode=Element.prototype.selectSingleNode=function(s){
    return this.selectNodes(s)[0];}
XMLDocument.prototype.selectNodes=Element.prototype.selectNodes=function(s){
    var rt=[];
    for(var i=0,rs=this.evaluate(s,this,this.createNSResolver(this.ownerDocument==null?this.documentElement:this.ownerDocument.documentElement),XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null),sl=rs.snapshotLength;i<sl;i++)
        rt.push(rs.snapshotItem(i));
    return rt;}

XMLDocument.prototype.__proto__.__defineGetter__("xml",function(){
    try{
        return new XMLSerializer().serializeToString(this);}
    catch(e){
        return document.createElement("div").appendChild(this.cloneNode(true)).innerHTML;}});
Element.prototype.__proto__.__defineGetter__("xml",function(){
    try{
        return new XMLSerializer().serializeToString(this);}
    catch(e){
        return document.createElement("div").appendChild(this.cloneNode(true)).innerHTML;}});

XMLDocument.prototype.__proto__.__defineGetter__("text",function(){
    return this.firstChild.textContent;});

Element.prototype.__proto__.__defineGetter__("text",function(){
    return this.textContent;});
Element.prototype.__proto__.__defineSetter__("text",function(s){
    return this.textContent=s;});

}

Javascript 相关文章推荐
jQuery实现鼠标经过图片变亮其他变暗效果
May 08 Javascript
JS实现三级折叠菜单特效,其它级可自动收缩
Aug 06 Javascript
JavaScript 性能优化小结
Oct 12 Javascript
jquery实现两边飘浮可关闭的对联广告
Nov 27 Javascript
使用node+vue.js实现SPA应用
Jan 28 Javascript
浅析在javascript中创建对象的各种模式
May 06 Javascript
angularjs实现时间轴效果的示例代码
Nov 29 Javascript
js将当前时间格式化为 年-月-日 时:分:秒的实现代码
Jan 20 Javascript
VSCode使用之Vue工程配置eslint
Apr 30 Javascript
JS实现选项卡效果的代码实例
May 20 Javascript
vue表单中遍历表单操作按钮的显示隐藏示例
Oct 30 Javascript
vue 把二维或多维数组转一维数组
Apr 24 Vue.js
javascript  Error 对象 错误处理
May 18 #Javascript
javascript:以前写的xmlhttp池,代码
May 18 #Javascript
JavaScript的9个陷阱及评点分析
May 16 #Javascript
认识延迟时间为0的setTimeout
May 16 #Javascript
用函数式编程技术编写优美的 JavaScript_ibm
May 16 #Javascript
Javascript模块模式分析
May 16 #Javascript
Dom加载让图片加载完再执行的脚本代码
May 15 #Javascript
You might like
php 获取可变函数参数的函数
2009/08/26 PHP
phpstorm编辑器乱码问题解决
2014/12/01 PHP
PHP自定义函数实现格式化秒的方法
2016/09/14 PHP
ThinkPHP实现静态缓存和动态缓存示例代码
2017/05/02 PHP
jquery 表单下所有元素的隐藏
2009/07/25 Javascript
自己做的模拟模态对话框实现代码
2012/05/23 Javascript
『jQuery』名称冲突使用noConflict方法解决
2013/04/22 Javascript
jq选项卡鼠标延迟的插件实例
2013/05/13 Javascript
详解jQuery插件开发中的extend方法
2013/11/19 Javascript
JavaScript中用于生成随机数的Math.random()方法
2015/06/15 Javascript
nodejs实现获取当前url地址及url各种参数值
2015/06/25 NodeJs
漂亮! js实现颜色渐变效果
2016/08/12 Javascript
浅谈html转义及防止javascript注入攻击的方法
2016/12/04 Javascript
前端开发必知的15个jQuery小技巧
2017/01/22 Javascript
ES6中module模块化开发实例浅析
2017/04/06 Javascript
浅析Vue项目中使用keep-Alive步骤
2018/07/27 Javascript
[01:03:09]完美世界DOTA2联赛PWL S2 Forest vs SZ 第二场 11.25
2020/11/26 DOTA
[01:15]PWL S2开团时刻第二期——他们杀 我就白给
2020/11/25 DOTA
Python RuntimeError: thread.__init__() not called解决方法
2015/04/28 Python
Python3.2模拟实现webqq登录
2016/02/15 Python
pandas数值计算与排序方法
2018/04/12 Python
Python中的四种交换数值的方法解析
2019/11/18 Python
Python中sys模块功能与用法实例详解
2020/02/26 Python
浅谈Python xlwings 读取Excel文件的正确姿势
2021/02/26 Python
css3 transform及原生js实现鼠标拖动3D立方体旋转
2016/06/20 HTML / CSS
GOOD AMERICAN官网:为曲线性感而设计
2017/12/28 全球购物
英国性感内衣和睡衣品牌:Bluebella
2018/01/26 全球购物
集团薪酬管理制度
2014/01/13 职场文书
《改造我们的学习》心得体会
2014/11/07 职场文书
2015年保险公司工作总结
2015/04/24 职场文书
时尚女魔头观后感
2015/06/04 职场文书
亮剑观后感
2015/06/05 职场文书
详解CSS玩转图片Base64编码
2021/05/25 HTML / CSS
Pytorch中Softmax和LogSoftmax的使用详解
2021/06/05 Python
SpringBoot整合Redis入门之缓存数据的方法
2021/11/17 Redis
SQL Server 中的事务介绍
2022/05/20 SQL Server