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 相关文章推荐
JavaScript操作XML 使用百度RSS作为新闻源示例
Feb 17 Javascript
JS实现点击文字对应DIV层不停闪动效果的方法
Mar 02 Javascript
jQuery遮罩层实现方法实例详解(附遮罩层插件)
Dec 08 Javascript
React.js入门学习第一篇
Mar 30 Javascript
用JS中split方法实现彩色文字背景效果实例
Aug 24 Javascript
Bootstrap面板使用方法
Jan 16 Javascript
Angularjs cookie 操作实例详解
Sep 27 Javascript
VUE2.0+ElementUI2.0表格el-table实现表头扩展el-tooltip
Nov 30 Javascript
p5.js实现简单货车运动动画
Oct 23 Javascript
JQuery中的常用事件、对象属性与使用方法分析
Dec 23 jQuery
easyUI 实现的后台分页与前台显示功能示例
Jun 01 Javascript
Vue-resource安装过程及使用方法解析
Jul 21 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 imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
php正则表达式(regar expression)
2011/09/10 PHP
php文字水印和php图片水印实现代码(二种加水印方法)
2013/12/25 PHP
php的mssql数据库连接类实例
2014/11/28 PHP
PHP模块memcached使用指南
2014/12/08 PHP
php中PDO方式实现数据库的增删改查
2015/05/17 PHP
IE8 原生JSON支持
2009/04/13 Javascript
前端开发部分总结[兼容性、DOM操作、跨域等](持续更新)
2010/03/04 Javascript
js一般方法改写成面向对象方法的无限级折叠菜单示例代码
2013/07/04 Javascript
js控制href内容的连接内容的变化示例
2014/04/30 Javascript
Javascript中call和apply函数的比较和使用实例
2015/02/03 Javascript
JS实现自适应高度表单文本框的方法
2015/02/25 Javascript
JavaScript Function函数类型介绍
2015/04/08 Javascript
jQuery 利用$.ajax 时获取原生XMLHttpRequest 对象的方法
2016/08/25 Javascript
BootStrap中的表单大全
2016/09/07 Javascript
vue使用技巧及vue项目中遇到的问题
2018/06/04 Javascript
原生JS无缝滑动轮播图
2019/10/22 Javascript
extjs图形绘制之饼图实现方法分析
2020/03/06 Javascript
python 写的一个爬虫程序源码
2016/02/28 Python
python图片验证码生成代码
2016/07/02 Python
Python实现简易Web爬虫详解
2018/01/03 Python
Python面向对象实现一个对象调用另一个对象操作示例
2019/04/08 Python
HTML5中的Article和Section元素认识及使用
2013/03/22 HTML / CSS
Lookfantastic瑞典:英国知名美妆购物网站
2018/04/06 全球购物
德国圣伯纳德草药屋:Kräuterhaus Sanct Bernhard(有中文站)
2018/08/05 全球购物
JVM是一个编译程序还是解释程序
2012/09/11 面试题
学生自我鉴定
2013/12/18 职场文书
数学与统计学院学生个人职业生涯规划书
2014/02/10 职场文书
最美乡村医生事迹材料
2014/06/02 职场文书
法人单位适用的授权委托书
2014/09/19 职场文书
个人查摆问题整改措施
2014/10/04 职场文书
销售员态度差检讨书
2014/10/26 职场文书
信访维稳承诺书
2015/05/04 职场文书
撤诉申请怎么写
2015/05/19 职场文书
2016年“9.22”世界无车日活动小结
2016/04/05 职场文书
MySQL自定义函数及触发器
2022/08/05 MySQL