我有一段 JavaScript 代码,可确保在加载页面时选择一个文本框。我在下面包含了我的代码:
<!doctype html>
<html>
<head>
<script type="text/javascript">
function box(query){
document.getElementById('query').focus();
}
</script>
</head>
<body onload="box('query');">
<input id="query">
</body>
</html>
我的问题是;如何让这个脚本在我的 body 标记中不加载的情况下工作?
我希望你能理解我想要描述的内容。
提前致谢,卡勒姆
最佳答案
将其放在结束符 </body>
内标签,这样 query
元素在运行之前已加载:
<!doctype html>
<html>
<head>
</head>
<body>
<input id="query">
<script type="text/javascript">
function box(query){
document.getElementById('query').focus();
}
box('query');
</script>
</body>
</html>
关于javascript - 使用 JavaScript 选择文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5994725/