$ .focus()不起作用 [英] $.focus() not working

查看:430
本文介绍了$ .focus()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的focus()文档状态的最后一个示例状态

The last example of jQuery's focus() documentation states

$('#id').focus()

应使输入集中(活动).我似乎无法正常工作.

should make the input focused (active). I can't seem to get this working.

即使在此站点的控制台中,我也在尝试将其用于搜索框

Even in the console on this site, I'm trying it for the search box

$('input[name="q"]').focus()

我什么也没得到.有什么想法吗?

and I'm getting nothing. Any ideas?

推荐答案

实际上,只要您不关注控制台,您给出的专注于此站点的示例就可以正常工作.不起作用的原因仅仅是因为它没有从开发控制台中窃取焦点.如果您在控制台中运行以下代码,然后在浏览器窗口中快速单击,您将看到它集中在搜索框中:

Actually the example you gave for focusing on this site works just fine, as long as you're not focused in the console. The reason that's not working is simply because it's not stealing focus from the dev console. If you run the following code in your console and then quickly click in your browser window after, you will see it focus the search box:

setTimeout(function() { $('input[name="q"]').focus() }, 3000);

关于您的另一个,过去给我带来麻烦的一件事是事件的顺序.您不能在尚未附加到DOM的元素上调用focus().您要关注的元素是否已附加到DOM?

As for your other one, the one thing that has given me trouble in the past is order of events. You cannot call focus() on an element that hasn't been attached to the DOM. Has the element you are trying to focus already been attached to the DOM?

这篇关于$ .focus()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆