2014-10-27 27 views
0

不確定我有以下功能:event.type在Firefox

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

正被呼籲的焦點事件,並在頁面加載。元素是輸入元素的值,p是一個可選的參數,我想執行if只在輸入元素上的焦點。如果我簡單地應用event.type =='focus',它適用於IE和Chrome,但是對於Firefox來說它說事件沒有被定義。

有沒有人有這方面的解決方法?提前致謝。

+3

您尚未爲函數定義'event'參數,並且FF沒有全局'window.event'對象。 – 2014-10-27 19:32:36

+1

Firefox不提供當前事件對象作爲全局變量。你必須將它傳遞給事件處理程序的函數。 – 2014-10-27 19:32:53

回答

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