无法在Chrome控制台中使用javascript设置焦点和光标以输入文本 [英] Unable to set focus and cursor to input text using javascript in Chrome Console

查看:164
本文介绍了无法在Chrome控制台中使用javascript设置焦点和光标以输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考 SO答案. 我正在web.whatsapp.com(chrome)上尝试相同的输入搜索字段. 这是我要执行的代码:

This is in reference to this SO answer. I am trying the same on web.whatsapp.com (chrome) for its input search field. Here is my code to do it:

document.getElementsByClassName("input input-search")[0].focus()
document.getElementsByClassName("input input-search")[0].select()

在Chrome控制台中,以上功能无效.

Above does not work from chrome console.

还有jQuery代码:

Also the jQuery code:

$(".input-search").focus() 

不起作用.执行上述方法后,仍然看不到光标的原因可能是什么?

does not work. What could be the reason that I don't see the cursor even after executing above methods?

推荐答案

我认为这与类,id,javascript或jQuery的问题无关.这是浏览器控制台的工作方式.运行每个命令后,控制台将获得焦点.因此,该焦点将不适用于来自控制台的其他输入.

I think this is not related to issue with class, id, javascript or jQuery. It's the way browser console works. The console gets focus after each command is run. So the focus will not work for other inputs from the console.

要对其进行测试,请在控制台中运行此代码.

To test it, run this code in console.

setTimeout(function(){$(".input.input-search").focus()},5000);

执行后,立即单击页面上的任意位置以将焦点移出控制台.现在,在5秒钟后,焦点将设置在输入上.

After executing it immediately click anywhere on the page to take focus out of console. Now after 5 seconds, the focus will set on input.

这篇关于无法在Chrome控制台中使用javascript设置焦点和光标以输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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