if(!document.all){
//textContent->text
    Element.prototype.__defineGetter__('text',function(){return this.textContent===undefined?this.text:this.nodeType?this.textContent:undefined;});
    Element.prototype.__defineSetter__('text',function(txt){this.textContent=txt;});

//让firefox文本节点支持text属性
    Text.prototype.__defineGetter__('text',function(){return this.textContent===undefined?this.text:this.nodeType?this.textContent:undefined;});
    Text.prototype.__defineSetter__('text',function(txt){ this.textContent = txt;});
    
//serializeToString->xml 只读
    Element.prototype.__defineGetter__('xml',function(){return this.nodeType?(new XMLSerializer()).serializeToString(this):null;});

//addEventListener->attachEvent
    HTMLElement.prototype.attachEvent = function(ev, fn){
        ev = ev.toLowerCase();
        this.addEventListener(ev.indexOf('on')===0?ev.substr(2):ev, fn, false);
    };
//removeEventListener->detachEvent
    HTMLElement.prototype.detachEvent = function(ev, fn){
        ev = ev.toLowerCase();
        this.removeEventListener(ev.indexOf('on')===0?ev.substr(2):ev,fn);
    };
    
    if(self!=top){ //为移动xWin定义事件,document为xWin下的document
      var doXwinMv=false; //用于修正移动xWin后其中的文本会被选中的问题
      document.addEventListener('mousedown', function(ev){ mouseOffset={x:ev.pageX, y:ev.pageY} });
      document.addEventListener('mousemove', function(ev){ top.mouseMove(ev); });
      document.addEventListener('mouseup', function(ev){ top.mouseUp(ev);if(window.getSelection() && doXwinMv) window.getSelection().collapseToStart(); doXwinMv=false;});
    }
    
    //修正firefox下xWin背景色为黑色的问题
    //if(!window.getComputedStyle(document.documentElement,null)['backgroundColor']) document.documentElement.style.backgroundColor = '#fff';
    //document.documentElement.style.backgroundColor = '#fff';

//让firefox支持innerText属性
    HTMLElement.prototype.__defineGetter__("innerText",function(){
        var textRange = this.ownerDocument.createRange();
        textRange.selectNodeContents(this);
        return textRange.toString().replace(/\t\n/g,'');
    });
    HTMLElement.prototype.__defineSetter__("innerText",function(str){
        this.textContent = str;
    });
    
//修正firefox下firstChild lastChild获取到的是空白行文本节点的问题
    Element.prototype.__defineGetter__('firstChild',function(){
    var firstEleNode = firstNode = null;
    firstNode = this.childNodes[0];
    while (firstNode && firstNode.nodeType===3 && firstNode.nodeValue=='\n'){//若是空白行文本节点 则继续往后查找
        firstNode = firstNode.nextSibling;
    }
    firstEleNode = firstNode;
    return firstEleNode;
    });
    
    Element.prototype.__defineGetter__('lastChild',function(){
        var mIdx = this.childNodes.length-1;
        var lastNode = lastEleNode =null;
        lastNode = this.childNodes[mIdx];
        while(lastNode && lastNode.nodeType===3 && lastNode.nodeValue==='\n'){//若是空白行文本节点 继续往前查找
            lastNode = lastNode.previousSibling;
        }
        lastEleNode = lastNode;
        return lastEleNode;
    });
    
//后台返回xml文档后,遍历节点会遍历到空白行文本节点 所以需要这个方法先删除空白行文本节点 属性节点也是子节点??
    Element.prototype.__defineGetter__('delEmptyLineNode',function(){ //注意:document文档节点没继承该属性 document.documentElemnt根节点继承了该属性
        function delEmpty(node){
            var chNodes=node.childNodes, re=/^\s+$|\n|\t/m;
            for(var i=0, len=chNodes.length; i<len; i++){
                if (chNodes[i] && chNodes[i].nodeType===1){
                    if(chNodes[i].childNodes.length===1){ //叶子节点
                        
                    }else{ //非叶子元素节点 递归
                        delEmpty(chNodes[i]);
                    }
                }else if(chNodes[i] && chNodes[i].nodeType===3 && re.test(chNodes[i].nodeValue)){//空白文本节点
                    node.removeChild(chNodes[i]);
                    --i;
                }
            }
        }
        delEmpty(this);
        return this;
    });
    
    
//xmlNode.selectNodes()方法 只支持ie, 在当前xml节点下查找节点, Firefox下为Element原型定义同名属性
    Element.prototype.selectNodes = function(xpathStr){
        if(xpathStr.charAt(0)=='/') xpathStr="."+xpathStr;
        var snapshot = this.ownerDocument.evaluate(xpathStr, this, null, 7, null);
        var nodes = [];
        for(var i=0; i<snapshot.snapshotLength; i++){
            nodes.push(snapshot.snapshotItem(i));
        }
        return nodes;
    };
    
    Element.prototype.selectSingleNode = function(xpathStr){
        return this.selectNodes(xpathStr)[0];
    };
//ie怪异模式下 可以node.children(2)这样获取第3个子节点,firefox或ie9等现代浏览器都不行,firefox修改同名属性children为children()方法来兼容
//注:children重定义后,children[0]这样的写法将不可用
    Object.defineProperty(Element.prototype, 'children',{value:function(i){return this.delEmptyLineNode.childNodes[i];}, writable:true, enumerable:true, configurable:true});

//(new DOMParser())).parseFromString(xmlStr, 'text/xml') --> loadXML
    XMLDocument.prototype.loadXML = function(xmlStr){
            return (new DOMParser()).parseFromString(xmlStr,'text/xml');
    };

//让firefox支持insertAdjacentElement
    HTMLElement.prototype.insertAdjacentElement = function(sWhere, insEle){
        if(!insEle.nodeType) return;
        this.insertAdjacentHTML(sWhere,insEle.xml);
    };
//让firefox支持ie事件对象的相关属性
//-- returnValue --
    Event.prototype.__defineSetter__("returnValue", function(b){
        if(!b) this.preventDefault();
        return b
    });
//-- cancelBubble --
    Event.prototype.__defineSetter__("cancelBubble", function(b){
        if(b) this.stopPropagation();
        return b;
    });
//--- srcElement ---
    Event.prototype.__defineGetter__("srcElement", function(){
        var node = this.target;
        while(node.nodeType !== 1) node = node.parentNode;
        return node;
    });
//--- fromElement ---
    Event.prototype.__defineGetter__("fromElement", function(){
        var node;
        if(this.type == 'mouseover'){ node = this.relatedTarget;}
        if(this.type == 'mouseout'){ node = this.target; }
        if(!node) return null;
        while(node.nodeType!=1){ node = node.parentNode; }
        return node;
    });
//--- toElement ---
    Event.prototype.__defineGetter__("toElement", function(){
        var node;
        if(this.type == 'mouseover'){ node = this.target; }
        if(this.type == 'mouseout' ){ node = this.relatedTarget; }
        if(!node) return null;
        while(node.nodeType!=1){ node = node.parentNode; }
        return node;
    });

//让firefox下table支持moveRow方法
    HTMLElement.prototype.moveRow = function(srcIdx, targetIdx){
        var re = /^(table|tbody|tfoot|thead)/i;
        if(!re.test(this.nodeName) || srcIdx === targetIdx) return;
        var pNode, srcR,targetR;
        pNode = this;
        if(this.nodeName.toLowerCase() === 'table') pNode = this.getElementsByTagName('tbody')[0]; //firefox 自动插入tbody
        //targetIdx<srcIdx 行往前面移 直接pNode.insertBefore()即可
        srcR = pNode.rows[srcIdx];
        targetR = pNode.rows[targetIdx];
        if(!srcR || !targetR) return; //索引范围以外 则返回
        targetRnext = pNode.rows[targetIdx+1] || null;
        if(targetIdx < srcIdx) pNode.insertBefore(srcR, targetR);
        if(targetIdx > srcIdx) pNode.insertBefore(srcR, targetRnext);
    };

//让firefox下的styleSheet对象支持rules属性    
    CSSStyleSheet.prototype.__defineGetter__('rules', function(){return this.cssRules});

//让firefox支持currentStyle 注意color样式值会转换为 rgb(233,22,22)的形式
    HTMLElement.prototype.__defineGetter__('currentStyle',function(){return window.getComputedStyle(this,null);});

//firefox createElementX
    HTMLDocument.prototype.createElementX = function (tag){
        var re=/^<.+>$/;
        if(re.test(tag)){ //<p ...></p> < ...>
            try{
                var tmpDiv = this.createElement('div'); //临时div
                tmpDiv.innerHTML = tag;
                return tmpDiv.firstChild;
            }catch(err){alert(err.message);}
        }else{ // p, div...
            try{
                return document.createElement(tag);
            }catch(e){alert(e.message)}
        }
    }
    
}//For Firefox End

原型扩展的方法解决IE和Firefox的Js兼容问题的更多相关文章

  1. JS高级---原型的引入,原型添加的方法解决数据共享

    原型的引入:解决:通过构造函数创建对象带来的问题,即浪费内存(一个对象开一个内存,多个对象开多个内存) 通过原型来添加方法,解决数据共享,节省内存空间 <script> function ...

  2. 解决selenium和FireFox版本不兼容问题

    相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器时会报错:org.openqa.selenium.firefox.NotConnectedException: Unab ...

  3. 解决selenium与firefox版本不兼容问题

    Python环境下类比 个人使用 32位环境 Python 2.7.12 Selenium 2.53.6 Firefox 47.01 安装selenium可用pip选择对应版本,参考另一教程. 因为在 ...

  4. IE和FireFox中JS兼容之event .

    event对象 IE 中可以直接使用 event 对象,而 FF 中则不可以,解决方法之一如下:var theEvent = window.event || arguments.callee.call ...

  5. [转]IE和FireFox中JS兼容之event .

    转载于:http://blog.csdn.net/jiachunfeng/article/details/6448186 http://justcoding.iteye.com/blog/587876 ...

  6. ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法

    ORA-01652:无法通过128(在表空间temp中)扩展temp段 解决方法 (2016-10-21 16:49:53)   今天在做一个查询的时候,报了一个"ORA-01652无法通过 ...

  7. (转)再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题)

    再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题) XML文档操作集锦(C#篇) webapi文档描述-swagger

  8. call()和原型继承的方法

    1.call() call()方法接受两个参数,obj和arg 比如functionA.call(obj,arg)   就是说现在运行(执行)functionA这个方法,但是functionA里面的方 ...

  9. 在Function对象上扩展method方法

    ;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...

随机推荐

  1. c/c++中与字符串处理相关的函数

    void *memccpy (void *dest, const void *src, int c, size_t n); 从src所指向的对象复制n个字符到dest所指向的对象中.如果复制过程中遇到 ...

  2. SQL Server JDBC驱动中sqljdbc和sqljdbc4区别

    为了支持向后兼容以及可能的升级方案,JDBC Driver 2.0 在每个安装包中都包括 2 个 JAR 类库:sqljdbc.jar 和 sqljdbc4.jar. qljdbc.jar 类库提供对 ...

  3. 最新版SDWebImage的使用

    我之前写过一篇博客,介绍缓存处理的三种方式,其中最难,最麻烦,最占内存资源的还是图片缓存,最近做的项目有大量的图片处理,还是采用了SDWebImage来处理,但是发现之前封装好的代码报错了.研究发现, ...

  4. 如何调试框架中的app

    1,在编写的app中添加断点,并重新生成或编译 2,找到框架app的相应位置代开文件把所用到的dll重新替换成上步生成的dll(bin->debug) 3,运行框架,在VS打开调试->附加 ...

  5. sql语句中特殊函数的用法

    1.concat CONCAT(字串1, 字串2, 字串3, ...): 将字串1.字串2.字串3,等字串连在一起. 例如: Geography 表格 region_name     store_na ...

  6. 深度优先搜索——迷宫问题(华为oj)

    题目描述: 定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, ...

  7. osg项目经验1<MFC+OSG中模型点选效果>

    点选主要是重载osg的GUIEventHandler, class CPickHandler : public osgGA::GUIEventHandler{ //自定义回调函数名:CPickHand ...

  8. aJax学习之Ajax工作原理

    转自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/18/2216553.html 在写这篇文章之前,曾经写过一篇关于AJAX技术的 ...

  9. jQuery 如何写插件 - 第一步

    这篇文章引自iteye,是老帖子了~~ 国外优秀的文也有,今天就看这位仁兄的吧,写的很到位啊,通俗易懂. jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相 ...

  10. 转 SSH框架搭建详细图文教程

    原址:http://blog.sina.com.cn/s/blog_a6a6b3cd01017c57.html 什么是SSH? SSH对应 struts spring hibernatestruts ...