繁体   English   中英

设置焦点后如何将我的 cursor 放入输入框?

[英]How can I get my cursor into the input box after I set the focus?

我在网站www.RedBubble.com上使用 javascript 将活动的 cursor 放置在顶部的搜索框中。 我可以通过代码让它聚焦,出现一个弹出窗口。 但是在我获得焦点后,我似乎无法让 cursor 出现在盒子内。

我运行以下命令:

let searchbar = document.querySelector('input[name="query"]');
const focusEvent = new Event('focus');
const selectEvent = new Event('select');
searchbar.dispatchEvent(focusEvent);
searchbar.dispatchEvent(select);

这导致搜索输入框获得焦点,但顽固地 cursor 不会在输入字段内闪烁。 如果我在页面上手动单击选项卡或手动单击框中,我会在框中同时获得焦点和 cursor。 但我似乎无法以编程方式重新创建它。

我还尝试在开发者控制台之外执行此操作,以防万一这是窃取焦点的原因,但无论是否激活开发者控制台,我都会得到相同的结果。

对于 cursor 焦点尝试下面的代码,它可能会有所帮助:

let searchbar = document.querySelector('input[name="query"]');
searchbar.focus();

此处接受的答案解释了调度事件不会生成与已调度事件关联的默认操作。

您需要明确调用焦点。

 let searchbar = document.querySelector('input[name="query"]'); searchbar.focus();
 <input name="query"/>

暂无
暂无

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

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