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 瀑布流 浮动布局(一)(延迟AJAX加载图片)
May 23 Javascript
js时间戳格式化成日期格式的多种方法
Nov 11 Javascript
Jquery api 速查表分享
Jan 12 Javascript
JavaScript实现数组在指定位置插入若干元素的方法
Apr 06 Javascript
js实现键盘上下左右键选择文字并显示在文本框的方法
May 07 Javascript
javascript精确统计网站访问量实例代码
Dec 19 Javascript
详解AngularJS中ng-src指令的使用
Sep 07 Javascript
轻松理解JavaScript之AJAX
Mar 15 Javascript
javascript 判断用户有没有操作页面
Oct 17 Javascript
vue权限问题的完美解决方案
May 08 Javascript
详解CocosCreator项目结构机制
Apr 14 Javascript
vue3获取当前路由地址
Feb 18 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
关于Intype一些小问题的解决办法
2008/03/28 PHP
ubuntu10.04配置 nginx+php-fpm模式的详解
2013/06/03 PHP
php生成数组的使用示例 php全组合算法
2014/01/16 PHP
php利用curl抓取新浪微博内容示例
2014/04/27 PHP
javascript+php实现根据用户时区显示当地时间的方法
2015/03/11 PHP
php数组生成html下拉列表的方法
2015/07/20 PHP
PHP并发多进程处理利器Gearman使用介绍
2016/05/16 PHP
通过PHP实现获取访问用户IP
2020/05/09 PHP
PHP哈希表实现算法原理解析
2020/12/11 PHP
js几个验证函数代码
2010/03/25 Javascript
JQuery DataTable删除行后的页面更新利用Ajax解决
2013/05/17 Javascript
JavaScript中的prototype和constructor简明总结
2014/04/05 Javascript
JavaScript代码编写中各种各样的坑和填坑方法
2014/06/06 Javascript
js正则表达式匹配数字字母下划线等
2015/04/14 Javascript
jQuery实现获取绑定自定义事件元素的方法
2015/12/02 Javascript
jquery跟随屏幕滚动效果的实现代码
2016/04/13 Javascript
全面解析Bootstrap中Carousel轮播的使用方法
2016/06/13 Javascript
浅谈JSON.stringify()和JOSN.parse()方法的不同
2016/08/29 Javascript
jQuery实现checkbox即点即改批量删除及中间遇到的坑
2017/11/11 jQuery
微信小程序实现判断是分享到群还是个人功能示例
2019/05/03 Javascript
Vue数据驱动表单渲染,轻松搞定form表单
2019/07/19 Javascript
vue-router结合vuex实现用户权限控制功能
2019/11/14 Javascript
JavaScript(js)处理的HTML事件、键盘事件、鼠标事件简单示例
2019/11/19 Javascript
javascript实现简单页面倒计时
2021/03/02 Javascript
Python pickle模块用法实例
2015/04/14 Python
Python查找数组中数值和下标相等的元素示例【二分查找】
2019/02/13 Python
python与pycharm有何区别
2020/07/01 Python
降低python版本的操作方法
2020/09/11 Python
宝拉珍选澳大利亚官方购物网站:Paula’s Choice澳大利亚
2016/09/13 全球购物
英国领先的葡萄酒专家:Majestic Wine
2017/05/30 全球购物
越南电子产品购物网站:FPT Shop
2017/12/02 全球购物
加拿大在线隐形眼镜和眼镜店:VisionPros
2019/10/06 全球购物
禁止酒驾标语
2014/06/25 职场文书
企业党员个人自我评价
2014/09/20 职场文书
5种方法告诉你如何使JavaScript 代码库更干净
2021/09/15 Javascript
Spring事务管理下synchronized锁失效问题的解决方法
2022/03/31 Java/Android