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 相关文章推荐
JS实现打开本地文件或文件夹
Mar 09 Javascript
使用node.js半年来总结的 10 条经验
Aug 18 Javascript
JavaScript中Window对象的属性及事件
Dec 25 Javascript
javascript九宫格图片随机打乱位置的实现方法
Mar 15 Javascript
微信小程序封装http访问网络库实例代码
May 24 Javascript
JavaScript之Date_动力节点Java学院整理
Jun 28 Javascript
自定义事件解决重复请求BUG的问题
Jul 11 Javascript
Vue filters过滤器的使用方法
Jul 14 Javascript
Vue-Access-Control 前端用户权限控制解决方案
Dec 01 Javascript
Vue事件修饰符native、self示例详解
Jul 09 Javascript
Vue项目中Api的组织和返回数据处理的操作
Nov 04 Javascript
JavaScript高级程序设计之变量与作用域
Nov 17 Javascript
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链接MySQL的常用扩展函数
2014/10/23 PHP
php分割合并两个字符串的函数实例
2015/06/19 PHP
PHP中Trait及其应用详解
2017/02/14 PHP
PHP自动补全表单的两种方法
2017/03/06 PHP
PHP微信网页授权的配置文件操作分析
2019/05/29 PHP
jQuery Ajax 全解析
2009/02/08 Javascript
jquery each()源代码
2011/02/14 Javascript
可插入图片的TEXT文本框
2013/12/27 Javascript
jquery预览图片实现鼠标放上去显示实际大小
2014/01/16 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
element ui 对话框el-dialog关闭事件详解
2018/02/26 Javascript
vue 实现复制内容到粘贴板clipboard的方法
2018/03/17 Javascript
在element-ui的select下拉框加上滚动加载
2019/04/18 Javascript
详解vuex数据传输的两种方式及this.$store undefined的解决办法
2019/08/26 Javascript
微信小程序页面间传递数组对象方法解析
2019/11/06 Javascript
vue 动态创建组件的两种方法
2020/12/31 Vue.js
python进阶教程之词典、字典、dict
2014/08/29 Python
Python中的闭包实例详解
2014/08/29 Python
python类继承与子类实例初始化用法分析
2015/04/17 Python
Python基于pygame实现图片代替鼠标移动效果
2015/11/11 Python
Windows安装Python、pip、easy_install的方法
2017/03/05 Python
在python3环境下的Django中使用MySQL数据库的实例
2017/08/29 Python
Python 记录日志的灵活性和可配置性介绍
2018/02/27 Python
在python 不同时区之间的差值与转换方法
2019/01/14 Python
Django项目创建到启动详解(最全最详细)
2019/09/07 Python
python 扩展print打印文件路径和当前时间信息的实例代码
2019/10/11 Python
python实现Oracle查询分组的方法示例
2020/04/30 Python
如何理解python面向对象编程
2020/06/01 Python
瑜伽国际:Yoga International
2018/04/18 全球购物
运动会演讲稿
2014/05/07 职场文书
美术课外活动总结
2014/07/08 职场文书
党性锻炼的心得体会
2014/09/03 职场文书
2014年大学生社会实践自我鉴定
2014/09/26 职场文书
护士长2014年度工作总结
2014/11/11 职场文书
小米11和iphone12哪个值得买?小米11对比iphone12评测
2021/04/21 数码科技
win10如何更改appdata文件夹的默认位置?
2022/07/15 数码科技