Home  >  Q&A  >  body text

javascript - How to make the search box of the navigation website automatically obtain the input cursor focus?

For example, this navigation website: https://www.2345.com/
After opening, the cursor did not automatically locate the Baidu search box.
I tried to get the id and call the focus function but it didn’t work.
How to write a piece of js code to automatically get focus every time you open this website?
The effect is like https://www.hao123.com/

PHP中文网PHP中文网2659 days ago753

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-06-12 09:30:26

    focus() should be fine, just write something like:

    <input id="t1" type="text" value="abc"><br>
    <input id="t2" type="text" value="def"><br>
    <script>
        t2.focus();
    </script>
    

    Can you post the code for a look?

    reply
    0
  • 代言

    代言2017-06-12 09:30:26

    Write the autofocus attribute directly on the input tag. Is it the onfocus() method in js?

    reply
    0
  • Cancelreply