Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery(document).on(';focus';)?_Javascript_Jquery_Javascript Events - Fatal编程技术网

Javascript jQuery(document).on(';focus';)?

Javascript jQuery(document).on(';focus';)?,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,每当用户关注输入上的单击时,我都想触发一个方法。 我有以下代码: jQuery(document).on('focus click', function(e){ console.log("focused on " + $(e.target).attr('id')); }); 但每当我关注一个元素时,它只会给出关注未定义的。此代码有什么问题?您在使用绑定事件时错过了输入选择器 on()的语法。on(事件[,选择器][,数据],处理程序(事件对象)) 尝试: 而且只要e.targe

每当用户关注输入上的单击时,我都想触发一个方法。 我有以下代码:

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

但每当我关注一个元素时,它只会给出关注未定义的
。此代码有什么问题?

您在使用绑定事件时错过了输入选择器

on()的语法。on(事件[,选择器][,数据],处理程序(事件对象))

尝试:


而且只要
e.target.id
就足够了。

甚至
this.id
就足够了;)。
jQuery(document).on('focus click', 'input',  function(e){
        console.log("focused on " + $(e.target).attr('id'));
});
$(document).on('focus click', 'input',  function(e){
        console.log("focused on " + e.target.id);
});