zoukankan      html  css  js  c++  java
  • IE和Firefox的兼容问题

    IE和Firefox的兼容问题

    本文摘自:http://blog.csdn.net/powerglover/archive/2009/01/15/3789631.aspx

     1、Event的问题
    在ie中我们可以直接使用event变量,但是在firefox下由于event是局部变量,firefox下我们可以事件绑定到元素上 例如
    <input type="button" onclick="doEvent (event)">   
    为了同时兼容ie和firefox 通常在函数种通过以下代码获得事件。
    var theEvent = window.event||e;
    var srcElement = theEvent.srcElement;
       if (!srcElement) {
            srcElement = theEvent.target;
       }

    2、滤镜问题
    在IE下是用.filters.alpha.opacity
    在Firefox下是用.style.opacity
    一般是来设置元素的透明度,所以我们一般通过以下代码来解决兼容问题
    var IE = navigator.userAgent.indexOf("MSIE")>0? 1: 0;
    if(IE)
    {
         obj.filters.alpha.opacity;
    }
    Else
    {
         obj.style.opacity;
    }

    3、innerText
    IE下我们经常使用innerText,但是Firefox不支持此写法,通常我们写成textContent. 它同时兼容IE和firefox,建议大家采用textContent.对于没有html标签的我们也可以采用innerHTML替代。

    4、event.srcElement
    IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event对象有target属性,但是没有srcElement属性.
    解决方法:使用obj(obj = event.srcElement ? event.srcElement : event.target;)

    5、 parentNode替代parentElement
    在IE中我可以通过obj.parentElement获得父元素,但是firex下不支持
    因为firefox与IE都支持DOM,所有我们可以采用obj.parentNode来解决。

    6、集合类对象问题
    IE下,可以使用()或[]获取集合类对象;Firefox下,只能使用[]获取集合类对象.
    解决方法:统一使用[]获取集合类对象.


    7、自定义属性问题
    说明:IE下,可以使用获取常规属性的方法来获取自定义属性,也可以使用getAttribute()获取自定义属性;Firefox下,只能使用getAttribute()获取自定义属性.
    解决方法:统一通过getAttribute()获取自定义属性.

    8、eval("idName")问题

    说明:IE下,,可以使用eval("idName")或getElementById("idName")来取得id为idName的HTML对象;Firefox下只能使用getElementById("idName")来取得id为idName的HTML对象.
    解决方法:统一用getElementById("idName")来取得id为idName的HTML对象.

    9、变量名与某HTML对象ID相同的问题
    说明:IE下,HTML对象的ID可以作为document的下属对象变量名直接使用;Firefox下则不能.Firefox下,可以使用与HTML对象ID相同的变量名;IE下则不能。
    解决方法:使用document.getElementById("idName")代替document.idName.最好不要取HTML对象ID相同的变量名,以减少错误;在声明变量时,一律加上var,以避免歧义.

    10、const问题
    说明:Firefox下,可以使用const关键字或var关键字来定义常量;IE下,只能使用var关键字来定义常量.
    解决方法:统一使用var关键字来定义常量.

    11、input.type属性问题
    说明:IE下input.type属性为只读;但是Firefox下input.type属性为读写.

    12、event.x与event.y问题

    说明:IE下,even对象有x,y属性,但是没有pageX,pageY属性;Firefox下,even对象有pageX,pageY属性,但是没有x,y属性.
    解决方法:使用mX(mX = event.x ? event.x : event.pageX;)来代替IE下的event.x或者Firefox下的event.pageX.


    13、window.location.href问题

    说明:IE或者Firefox2.0.x下,可以使用window.location或window.location.href;Firefox1.5.x下,只能使用window.location.
    解决方法:使用window.location来代替window.location.href.

    14、模态和非模态窗口问题

    说明:IE下,可以通过showModalDialog和showModelessDialog打开模态和非模态窗口;Firefox下则不能.
    解 决方法:直接使用window.open(pageURL,name,parameters)方式打开新窗口。如果需要将子窗口中的参数传递回父窗口,可 以在子窗口中使用window.opener来访问父窗口. 例如:var parWin = window.opener; parWin.document.getElementById("Aqing").value = "Aqing";

    15、frame问题

    以下面的frame为例:
    <frame src="/xxx.html" id="frameId" name="frameName" />

    (1)访问frame对象:
    IE:使用window.frameId或者window.frameName来访问这个frame对象.
    Firefox:只能使用window.frameName来访问这个frame对象.
    另外,在IE和Firefox中都可以使用window.document.getElementById("frameId")来访问这个frame对象.

    (2)切换frame内容:
    在IE和Firefox中都可以使用window.document.getElementById("testFrame").src = "xxx.html"或window.frameName.location = "xxx.html"来切换frame的内容.

    如果需要将frame中的参数传回父窗口,可以在frme中使用parent来访问父窗口。例如:parent.document.form1.filename.value="Aqing";

    16、body问题

    Firefox的body在body标签没有被浏览器完全读入之前就存在;而IE的body则必须在body标签被浏览器完全读入之后才存在.

    例如:
    Firefox:
    <body>
    <script type="text/javascript">
    document.body.onclick = function(evt){
    evt = evt || window.event;
    alert(evt);
    }
    </script>
    </body>
    IE&Firefox:
    <body>
    </body>
    <script type="text/javascript">
    document.body.onclick = function(evt){
    evt = evt || window.event;
    alert(evt);
    } </script>

    17、 事件委托方法
    IE:document.body.onload = inject; //Function inject()在这之前已被实现
    Firefox:document.body.onload = inject();
    document.body.onload=new Function('inject()');

    18、cursor:hand 和 cursor:pointer
    firefox不支持hand,但ie支持pointer
    解决方法: 统一使用pointer
    19、 FireFox中类似 obj.style.height = imgObj.height 的语句无效

    解决方法:
    obj.style.height = imgObj.height + 'px';

    20、ie,firefox以及其它浏览器对于 table 标签的操作都各不相同,在ie中不允许对table和tr的innerHTML赋值,使用js增加一个tr时,使用appendChile方法也不管用。

    解决方法:
    //向table追加一个空行:
    var row = otable.insertRow(-1);
    var cell = document.createElement("td");
    cell.innerHTML = " ";
    cell.className = "XXXX";
    row.appendChild(cell);

    21、 padding 问题

    padding 5px 4px 3px 1px FireFox无法解释简写,必须改成 padding-top:5px; padding-right:4px; padding-bottom:3px; padding-left:1px;

    22、消除ul、ol等列表的缩进时

    样式应写成:list-style:none;margin:0px;padding:0px;
    其中margin属性对IE有效,padding属性对FireFox有效

    23、CSS透明

    IE:filter:progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=60)。
    FF:opacity:0.6。

    24、CSS圆角

    IE:不支持圆角。
    FF: -moz-border-radius:4px,或者-moz-border-radius-topleft:4px;-moz-border- radius-topright:4px;-moz-border-radius-bottomleft:4px;-moz-border-radius- bottomright:4px;。

    25、CSS双线凹凸边框

    IE:border:2px outset;。
    FF: -moz-border-top-colors: #d4d0c8 white;-moz-border-left-colors: #d4d0c8 white;-moz-border-right-colors:#404040 #808080;-moz-border-bottom-colors:#404040 #808080;
    26、ie支持document.all 而firefox 不支持
    改用下面三个tag的其中一个来代替document.all
    getElementsByTagName("tagName") 可以得到得到所有标签元素的集合
    getElementById("idName")          可以按id得到某一元素
    getElementsByName("Name")            可以得到按name属性得到某一元素
    27、firefox 中使用innerHTML 的方法
    <div id="online"></div>
    document.all.online.innerHTML; //这种方法在IE中可以使用,但不是标准方法
    document.getElementById("online").innerHTML; //这样firefox就能使用innerHTML了
    28、eval()与window.execScript()执行脚本
    IE、firerox均支持eval(),firefox不支持window.execScript()
    解决:统一使用eval()
    29、e.button键值有别于event.button,只有3个键值而无组合键值


    30、insertAdjacentHTML 和 insertAdjacentText
    insertAdjacentHTML 和 insertAdjacentText 是IE下特有的JS,功能非常好用

    可惜Firefox 没有这两东东,不过,加上下面的这段的,Firefox下也可以支持这

    两个方法了

    if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
    {
         HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
         {
            switch (where)
            {
                case 'beforeBegin':
                    this.parentNode.insertBefore(parsedNode,this)
                    break;
                case 'afterBegin':
                    this.insertBefore(parsedNode,this.firstChild);
                    break;
                case 'beforeEnd':
                    this.appendChild(parsedNode);
                    break;
                case 'afterEnd':
                 if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
                        else this.parentNode.appendChild(parsedNode);
                    break;
             }
         }

         HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr)
         {
             var r = this.ownerDocument.createRange();
             r.setStartBefore(this);
             var parsedHTML = r.createContextualFragment(htmlStr);
             this.insertAdjacentElement(where,parsedHTML)
         }

         HTMLElement.prototype.insertAdjacentText = function (where,txtStr)
         {
             var parsedText = document.createTextNode(txtStr)
             this.insertAdjacentElement(where,parsedText)
         }
    }
    31、elementFromPoint

    Ie下有elementFromPoint方法,但是firefox没有,可以重写该方法

    Document.prototype.elementFromPoint = function(x, y)
         {
         this.addEventListener("mousemove", this.elementFromPoint__handler, false);
         var event = this.createEvent("MouseEvents");
         var box = this.getBoxObjectFor(this.documentElement);
         var screenDelta = { x: box.screenX, y: box.screenY };
         event.initMouseEvent("mousemove", true, false, this.defaultView, 0,
         x + screenDelta.x, y + screenDelta.y, x, y,
         false, false, false, false, 0, null);
         this.dispatchEvent(event);
         this.removeEventListener("mousemove", this.elementFromPoint__handler, false);
         return this.elementFromPoint__target;
         }
         Document.prototype.elementFromPoint__handler = function (event)
         {
         this.elementFromPoint__target = event.explicitOriginalTarget;

         if (this.elementFromPoint__target.nodetype == Node.TEXT_NODE)
         this.elementFromPoint__target = this.elementFromPoint__target.parentNode;

         if (this.elementFromPoint__target.nodeName.toUpperCase() == "HTML" && this.documentElement.nodeName.toUpperCase() == "HTML")
         this.elementFromPoint__target = this.getElementsByTagName("BODY").item(0);

         //****added this code to check for textboxes and textareas
         if ( this.elementFromPoint__target.nodeName=="#document" )//possible textbox or textarea
         {
         rp = event.rangeParent;
         alert("event.rangeParent = " + rp);
         if ( event.rangeParent.nodetype == Node.TEXT_NODE )//textbox with a value
         this.elementFromPoint__target = event.rangeParent.parentNode.parentNode;
         else if ( event.rangeParent.nodeName == 'div' )//textbox without a value
         this.elementFromPoint__target = event.rangeParent.parentNode;
         }
         //****end. However this cause permission denied as the rangeParent object appears to be private!

         event.preventdefault();
         event.stopPropagation();
         }
         Document.prototype.elementFromPoint__target = null;

    32、mousewheel事件
    firefox 没有 mousewheel 事件。可以通过以下方法解决.
    <script>
    var n=0;
    function mwEvent(e)
    {
    if (!e) e = window.event;
    if ( e.wheelDelta <= 0 || e.detail > 0) { n++; }
    else { n--; }
    window.status=n;
    }
    if(document.attachEvent){
    document.attachEvent("onmousewheel",mwEvent);
    }else{
    window.addEventListener("DOMMouseScroll", mwEvent, false);
    }
    </script>

    33、IE和FireFox的鼠标滚轮事件
    滚轮IE和Firefox的代码不一样:
    IE是mousewheel事件,Firefox是DOMMouseScroll事件
    事件属性,IE是event.wheelDelta,Firefox是event.detail
    属性的方向值也不一样,IE向上滚 > 0,Firefox向下滚 > 0
    //滚轮放大或缩小,基于Prototype 1.6
    var scrollfunc = function(event) {
    var direct = 0;
            if (event.wheelDelta) {
                    direct = event.wheelDelta > 0 ? 1 : -1;
            } else if (event.detail) {
                    direct = event.detail < 0 ? 1 : -1;
            }
            zoom(direct);
    };
    Event.observe(document, 'mousewheel', scrollfunc);
    Event.observe(document, 'DOMMouseScroll', scrollfunc); //firefox
    34、attachEvent方法

    attachEvent方法解释:
    attachEvent有2个参数,第一个参数是事件名,第二个参数是事件触发后所响应的方法. Firefox下解决方法是
    Object.prototype.attachEvent=function(method,func)
    {
    if(!this[method])
      this[method]=func;
    else
      this[method]=this[method].attach(func);
    }
    Function.prototype.attach=function(func){
    var f=this;
    return function(){
      f();
      func();
    }
    }
    36 、title替代alt
    在firefox和ie尽力都用title,alt在firefox下不起作用

     

    37. IE和Firefox在DOM解析childNodes上的不同


     

     


    <html><body>
    <UL id="main">
      <LI>1</LI>
      <LI>2</LI>
    </UL>
    <input type=button value="click me!"
      onclick="alert(document.getElementById('main').childNodes.length)">
    </body></html>
    呵呵,在IE里提示2,在Firefox里提示5,这是怎么回事呢?UL下明明只有两个子节点,Firefox为什么提示5个子节点呢?通过查看IE和firefox的DOM结构,发现了问题的所在:
    IE: <ul>

             <li>  

                   #text

     

             <li>  

                   #text

     

     

    Firefox:<ul>

                      #text

                      <li>  

                           #text

     

                      <li>  

                           #text

        
    你也应该发现了吧?“#text”表示文本(实际是无意义的空格和换行等)在Firefox里也会被解析成一个节点,在IE里只有有实际意义的文本才会解析成“#text”。所以对firefox而言,上例中UL的fistChild并不是第一个LI,而是#text(UL和第一个LI之间的无意义文本),lastChild也并不是最后一个LI,而是最后一个LI和/UL之间的无意义文本。

    解决办法是先用 .getElementsByTagName(”LI”) 得到所有LI的节点。我觉得firefox这样处理比较令程序员头痛,也没有必要。



    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/powerglover/archive/2009/01/15/3789631.aspx

  • 相关阅读:
    各种推导式玩法
    游标实例
    点滴纪录
    Jquery和CSS--点滴精华总结
    Asp.net页面之间几种传值方法【示例】
    .net Repeater知识知多少
    SqlServer 知识标记
    从客户端(...)中检测到有潜在危险的Request.Form 值的处理办法
    Jquery获取html中select,radiobutton选中的值写法
    Javascript获取当前日期
  • 原文地址:https://www.cnblogs.com/pricks/p/1495290.html
Copyright © 2011-2022 走看看