javascript - 使用 JavaScript 选择文本框

标签 javascript html

我有一段 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/

相关文章:

javascript - jquery 从父级引用这个

javascript - 如何在 IE9 中使用 jQuery 通过 anchor 提交表单

html - <label> HTML 元素问题

javascript - 如何在没有键盘的情况下使用javascript制作键盘事件

javascript - 不同功能的滑动切换不起作用

javascript - 为什么我的滚动条与我想要的相反?

javascript - 如何检查 AngularJS 模板中的 isObject

javascript - Node 功能不中断循环

javascript - 在所有页面中包含 Menu.html

html - 如何在 bootstrap 4 中包装( float )卡片