Focus Input Box On Load

后端 未结 11 2537
无人及你
无人及你 2020-11-30 01:10

How can the cursor be focus on a specific input box on page load?

Is it posible to retain initial text value as well and place cursor at end of input?



        
相关标签:
11条回答
  • 2020-11-30 01:37

    Add this to the top of your js

    var input = $('#myinputbox');
    
    input.focus();
    

    Or to html

    <script>
        var input = $('#myinputbox');
    
        input.focus();
    </script>
    
    0 讨论(0)
  • 2020-11-30 01:40

    Try:

    Javascript Pure:

    [elem][n].style.visibility='visible';
    [elem][n].focus();
    

    Jquery:

    [elem].filter(':visible').focus();
    
    0 讨论(0)
  • 2020-11-30 01:44

    Just a heads up - you can now do this with HTML5 without JavaScript for browsers that support it:

    <input type="text" autofocus>
    

    You probably want to start with this and build onto it with JavaScript to provide a fallback for older browsers.

    0 讨论(0)
  • 2020-11-30 01:44
    $(document).ready(function() {
        $('#id').focus();
    });
    
    0 讨论(0)
  • 2020-11-30 01:46

    very simple one line solution:

    <body onLoad="document.getElementById('myinputbox').focus();">
    
    0 讨论(0)
提交回复
热议问题