javascript 如何让页面加载时文本输入框光标自动闪烁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20769235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:15:27  来源:igfitidea点击:

How do I make the text input box cursor blink automatically when the page loads?

javascripthtmlcssautofocus

提问by mango

I want the cursor/line thing in the text box to automatically start blinking when the page loads. I've tried

我希望文本框中的光标/行在页面加载时自动开始闪烁。我试过了

contentEditable="true"

but that's not working

但这不起作用

回答by Scary Wombat

You have to set focus on the text box.

您必须将焦点设置在文本框上。

This can be done either by

这可以通过

JavaScript document.getElementById("fname").focus();

JavaScript document.getElementById("fname").focus();

or

或者

Jquery $( "#fname" ).focus();

查询 $( "#fname" ).focus();

or

或者

HTML5 <input type="text" name="fname" autofocus />

HTML5 <input type="text" name="fname" autofocus />

回答by Sohail Arif

In HTML5, you can simply use the input autofocus attribute.

在 HTML5 中,您可以简单地使用 input autofocus 属性。

However, beware that this does not work in some versions of IE.

但是,请注意这在某些版本的 IE 中不起作用。