search

Home  >  Q&A  >  body text

javascript - How to make the input box automatically gain focus in js?

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状态
我想大声告诉你我想大声告诉你2729 days ago786

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:33:40

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-12 09:33:40

    trigger, simulate click

    reply
    0
  • Cancelreply