分享

书写同时兼容IE和FireFox的事件event处理代码

 jasonliang 2012-10-11
1.在ie中,事件对象是作为一个全局变量来保存和维护的。 所有的浏览器事件,不管是用户触发的,还是其他事件, 都会更新window.event 对象。 所以在代码中,只要轻松调用 window.event 就可以轻松获取事件对象, 再event.srcElement 就可以取得触发事件的元素进行进一步处理。

2.在ff中, 事件对象却不是全局对象,一般情况下,是现场发生,现场使用,ff把事件对象自动传
递给对应的事件处理函数。在代码中,函数的第一个参数就是ff下的事件对象了。

以上是我个人对两个浏览器下的事件处理方法的粗浅理解,可能说得不是很明白,我写些代码来详细说明一下。
<button id="btn1">按钮1</button>
<button id="btn2">按钮2</button>
<button id="btn3">按钮3</button>
<script>
 
 window.onload=function(){
     document.getElementById("btn1").onclick=foo1
     document.getElementById("btn2").onclick=foo2
     document.getElementById("btn3").onclick=foo3
 }
 
 function foo1(){
      //ie中, window.event使全局对象
     alert(window.event)   // ie下,显示 "[object]" ,  ff下显示 "undefined"
    
     //ff中,  第一个参数自动从为 事件对象
     alert(arguments[0])   // ie下,显示  "undefined",  ff下显示 "[object]"
 }
 
 function foo2(e){
     alert(window.event)  // ie下,显示 "[object]" ,  ff下显示 "undefined"
    
     //注意,我从来没有给  foo2传过参数哦。  现在 ff自动传参数给 foo2, 传的参数e 就是事件对象了
     alert(e)               // ie下,显示  "undefined",  ff下显示 "[object]"
 }
 
 function foo3(){    //同时兼容ie和ff的写法,取事件对象
     alert(arguments[0] || window.event)  // ie 和 ff下,都显示 "[object]"
     var evt=arguments[0] || window.event
     var element=evt.srcElement || evt.target  //在 ie和ff下  取得 btn3对象
     alert(element.id)           //    btn3
 }
</script>

看到这里,我们似乎对 ie和ff的事件处理方式都已经理解了,并找到了解决的办法。 但是。。。。事情还没有结束。看代码
<button id="btn" onclick="foo()">按钮1</button>
<script>
function foo(){  
    alert(arguments[0] || window.event)
}
</script>

很不幸,我们 foo给我们的结果是 undefined, 而不是期望的 object

原因在于事件绑定的方式

onclick="foo()" 就是直接执行了, foo() 函数,没有任何参数的,这种情况下firefox没有机会传递任何参数给foo而 btn.onclick=foo 这种情况, 因为不是直接执行函数,firefox才有机会传参数给foo

解决方法:

方法一:比较笨的方法,既然 firefox没有机会传参数,那么自己勤快点,自己传
<button id="btn" onclick="foo(event)">按钮</button>
<script>
function foo(){  
   alert(arguments[0] || window.event)       
   var evt=arguments[0] || window.event
   var element=evt.srcElement || evt.target 
   alert(element.id)  
}
</script>

方法二: 自动查找
<button id="btn4" onclick="foo4()">按钮4</button>
 <script>
 function foo4(){  
     var evt=getEvent()
     var element=evt.srcElement || evt.target 
     alert(element.id)        
 }
 
 function getEvent(){     //同时兼容ie和ff的写法
         if(document.all)    return window.event;       
         func=getEvent.caller;           
         while(func!=null){   
             var arg0=func.arguments[0];
             if(arg0){
                 if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
                     || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){   
                     return arg0;
                 }
             }
             func=func.caller;
         }
         return null;
 }
 </script>

方法二由 lostinet原创,我在其基础上有所改进, 原函数如下
function SearchEvent()
{
    //IE
    if(document.all)
        return window.event;
       
    func=SearchEvent.caller;
    while(func!=null)
    {
        var arg0=func.arguments[0];
        if(arg0)
        {
            if(arg0.constructor==Event)
                return arg0;
        }
        func=func.caller;
    }
    return null;
}

简单总结:

以上两个解决方法中,都正确处理 ff和ie下 的事件处理 (不管是onclick="foo()",方式还是 onclick=foo方式)但是个人建议用 getEvent() 方法来统一处理事件问题。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多