ten*_*tar 312 html javascript textbox focus
我有一个带有文本框的Web表单.如何默认将焦点设置到文本框?
这样的事情:
<body onload='setFocusToTextBox()'>
Run Code Online (Sandbox Code Playgroud)
所以有人可以帮我吗?我不知道如何使用JavaScript将焦点设置到文本框.
<script>
function setFocusToTextBox(){
//What to do here
}
</script>
Run Code Online (Sandbox Code Playgroud)
moh*_*han 542
做这个.
如果你的元素是这样的..
<input type="text" id="mytext"/>
Run Code Online (Sandbox Code Playgroud)
你的脚本会是
<script>
function setFocusToTextBox(){
document.getElementById("mytext").focus();
}
</script>
Run Code Online (Sandbox Code Playgroud)
a b*_*ver 133
对于它的价值,您可以autofocus在兼容HTML5的浏览器上使用该属性.从版本10开始,甚至可以在IE上运行.
<input name="myinput" value="whatever" autofocus />
Run Code Online (Sandbox Code Playgroud)
Kha*_* TO 63
通常当我们关注文本框时,我们也应该滚动到视图中
function setFocusToTextBox(){
var textbox = document.getElementById("yourtextbox");
textbox.focus();
textbox.scrollIntoView();
}
Run Code Online (Sandbox Code Playgroud)
检查它是否有帮助.
Ric*_*eco 27
如果您的代码是:
<input type="text" id="mytext"/>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是JQuery,您也可以使用它:
<script>
function setFocusToTextBox(){
$("#mytext").focus();
}
</script>
Run Code Online (Sandbox Code Playgroud)
请记住,您必须先绘制输入 $(document).ready()
小智 20
对于普通的Javascript,请尝试以下操作:
window.onload = function() {
document.getElementById("TextBoxName").focus();
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
744012 次 |
| 最近记录: |