javascript - How to make the input box automatically gain focus in js?
我想大声告诉你
我想大声告诉你 2017-06-12 09:31:40
0
2
795

element.focus() is useless, there is no focus effect with cursor flashing, what should I do?

<input autofocus="autofocus" id="input">
//js
document.getElementById('input').focus();//我执行这句语句的时候光标没有自动聚焦,还是blur状态
我想大声告诉你
我想大声告诉你

reply all(2)
Ty80

autofocus HTML5
This Boolean attribute lets you specify that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form element in a document can have the autofocus attribute, which is a Boolean. It cannot be applied if the type attribute is set to hidden (that is, you cannot automatically set focus to a hidden control).

Just add this attribute to the input box
mdn link


Also I don’t understand why element.focus() is useless. It can't be solved without posting the code. It will be useful to write a test.

Ty80

trigger, simulate click

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template