简体   繁体   English

焦点在输入字段

[英]focus in a input field

After accepting the CPM I would like to have the cursor in the field that is in the center of this page: https://www.openthesaurus.de/?test=1 but unfortunately, every piece of code I try in the console doesn't work.接受 CPM 后,我希望光标位于此页面中心的字段中: https : //www.openthesaurus.de/?test=1但不幸的是,我在控制台中尝试的每段代码都没有不行。

I've tried with我试过

document.getElementById("search-field").focus()
document.getElementById("search-field").click()

and some other options that I've seen in some answers here, but none of them really worked.以及我在此处的一些答案中看到的其他一些选项,但它们都没有真正起作用。

Can someone provide me some light in this darkness?有人能在这黑暗中给我一些光明吗?

It actually is working.它实际上正在工作。 The problem is that your text focus is in the dev console.问题是您的文本焦点位于开发控制台中。

To illustrate this, try the same thing but with a timeout.为了说明这一点,请尝试相同的操作,但要超时。 Paste and run this in your dev console, then quickly click into the page to give it focus.在你的开发控制台中粘贴并运行它,然后快速点击进入页面以使其成为焦点。 After a few seconds you'll see the input get focus.几秒钟后,您将看到输入获得焦点。

setTimeout(() => document.querySelector('#search-field').focus(), 5000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM