firefox中未定义event.type

event.type undefined in firefox

本文关键字:type event 未定义 firefox      更新时间:2023-09-26

我有以下功能:

format : function(element,p){
 //function body
 if(event.type == 'focus'){
   //execute this condition only on focus event 
 }
}

在焦点事件和页面加载时调用。element是输入元素的值,p是一个可选的参数,我想只对输入元素的焦点执行if。如果我只是简单地应用event.type=='focus',它适用于IE和chrome,但对于firefox,它表示未定义事件。

有人能解决这个问题吗?提前谢谢。

var myObj = {
    format: function (event) {
        // passing in the event object should give you everything you need:
        // event.type (the event's type)
        // event.target (the element which the event was fired on)
        // event.which (in the case of keyboard events, this is the key that was pressed)
        // see mdn dox @ https://developer.mozilla.org/en-US/docs/Web/API/Event
        if (event.type === 'focus') {
            // execute focus code
        }
    }
};