Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.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
jquery触发器焦点在chrome中不起作用_Jquery_Google Chrome_Triggers_Safari_Focus - Fatal编程技术网

jquery触发器焦点在chrome中不起作用

jquery触发器焦点在chrome中不起作用,jquery,google-chrome,triggers,safari,focus,Jquery,Google Chrome,Triggers,Safari,Focus,我在我的网站上有一个表单,我填写了get请求变量的所有字段。在输入字段的第2个字段中,我正在执行谷歌地图应用程序以计算距离 当我读取请求中的值时,我想触发焦点事件,以便自动调用gmaps应用程序并计算距离。触发器功能有时在chrome和safari中不起作用,尽管它在Firefox、explorer和edge中始终有效。我正在尝试设置超时,但问题仍然存在 我的代码 setTimeout(function() { $('#option_31').trigger("focus");

我在我的网站上有一个表单,我填写了get请求变量的所有字段。在输入字段的第2个字段中,我正在执行谷歌地图应用程序以计算距离

当我读取请求中的值时,我想触发焦点事件,以便自动调用gmaps应用程序并计算距离。触发器功能有时在chrome和safari中不起作用,尽管它在Firefox、explorer和edge中始终有效。我正在尝试设置超时,但问题仍然存在

我的代码

setTimeout(function() {
    $('#option_31').trigger("focus");
    $('#option_31').on("focus", function(){
        //do staff
    });
    $('#option_32').trigger("focus");
    $('#option_32').on("focus", function(){
        //do staff
    });
}, 2000);

您正试图在设置事件处理程序之前触发超时。 某些平台上的某些浏览器在触发事件处理程序时可能太慢,或者在设置事件处理程序时太快,因此您的代码可以在那里工作

最好不要在超时内设置事件处理程序:

$('#option_31').on("focus", function(){
    //do stuff
});     
$('#option_32').on("focus", function(){
    //do stuff
});

setTimeout(function() {
  $('#option_31').trigger("focus");
  $('#option_32').trigger("focus");
}, 2000);

不要忘记将代码放入$function{your code}中,以确保在DOM就绪时执行它