2013-02-18 83 views
2

每當用戶關注點擊輸入時,我想觸發一個方法。 我有以下代碼:jQuery(document).on('focus')?

jQuery(document).on('focus click', function(e){ 
     console.log("focused on " + $(e.target).attr('id')); 
}); 

但每當我焦點的元素上它只是給focused on undefined。這段代碼有什麼問題?

回答

4

使用on()錯過了綁定事件中的輸入選擇器。對()。對

jQuery(document).on('focus click', 'input', function(e){ 
     console.log("focused on " + $(e.target).attr('id')); 
}); 

語法(事件[,選擇器] [,數據],處理程序(eventObject)傳遞),reference

3

嘗試:

$(document).on('focus click', 'input', function(e){ 
     console.log("focused on " + e.target.id); 
}); 

也只是e.target.id就夠了..

+2

即使'this.id '足夠了;)。 – Simon 2013-02-18 12:10:05

+0

@Simon你是對的.. – Anujith 2013-02-18 12:10:57