Javascript jQuery(document).on ('focus')?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14935838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery(document).on ('focus')?
提问by user961627
Whenever the user focuses on clicks on an input, I want to trigger a method. I have the following code:
每当用户专注于点击输入时,我想触发一个方法。我有以下代码:
jQuery(document).on('focus click', function(e){
console.log("focused on " + $(e.target).attr('id'));
});
But whenever I focus on an element it just gives focused on undefined. What's wrong with this code?
但是每当我关注一个元素时,它只会给出focused on undefined. 这段代码有什么问题?
回答by Adil
You missed the input selector in bind event using on().
您使用on()错过了绑定事件中的输入选择器。
jQuery(document).on('focus click', 'input', function(e){
console.log("focused on " + $(e.target).attr('id'));
});
syntax of on().on( events [, selector ] [, data ], handler(eventObject) ), reference
on().on( events [, selector ] [, data ], handler(eventObject) ) 的语法,参考
回答by Anujith
Try:
尝试:
$(document).on('focus click', 'input', function(e){
console.log("focused on " + e.target.id);
});
Also just e.target.idis enough..
也e.target.id刚够。。

