Focus Input Box On Load

2019-01-13 09:06发布

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?

<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" value = "initial text" />

9条回答
男人必须洒脱
2楼-- · 2019-01-13 09:45

Try:

Javascript Pure:

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

Jquery:

[elem].filter(':visible').focus();
查看更多
Emotional °昔
3楼-- · 2019-01-13 09:48
$(document).ready(function() {
    $('#id').focus();
});
查看更多
smile是对你的礼貌
4楼-- · 2019-01-13 09:55

Code snippets:


function focusOnMyInputBox(){
    document.getElementById("myinputbox").focus();
}

<body onLoad="focusOnMyInputBox()")>


<input type="text"  size="25" id="myinputbox" class="input-text" name="input2" onfocus="this.value = this.value;" value = "initial text" />

查看更多
登录 后发表回答