码迷,mamicode.com
首页 > 其他好文 > 详细

同时兼容IE和Firefox的事件(Event)

时间:2015-05-07 18:23:02      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

例子:

function TxtKeywordKeyDown()
{
    var evt = getEvent();
    
    if(evt.keyCode == 13)
    {
        SearchRedirect();
    }
    if(evt.keyCode == 38)
    {
        alert("向上");
    }
    if(evt.keyCode == 40)
    {
        alert("向下");
    }
}

 

同时兼容IE和Firefox的事件(Event)

标签:

原文地址:http://www.cnblogs.com/wayne23/p/4485540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!