为什么 onload 焦点在此脚本上不起作用?

发布于 2024-10-31 23:30:52 字数 932 浏览 1 评论 0原文

这是我的表格样本。当页面加载时,焦点必须位于“fbox”上,但它不起作用,我不明白为什么。该表单包含一个好的编辑器,但我不认为这是问题所在,

<html>
<head></head>
<body onload="document.form.fbox.focus();">
<body>
<form method='post' action='' name='form' >
Headline <input name='fbox' type='text' class='form' id='box' autocomplete='off' size='80'><br>
Your text</font><br><script type="text/javascript" src="nicEdit.js"></script><script type="text/javascript">bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });</script>
<textarea name="description" style="width: 100%; height:200px;"></textarea></p>     
<p><select name='catg' >
<option value='' selected >Select category</option>
</select>
<input type="submit" id='button' name="Submit" value="Submit" class="button"></form>
</body></html>

谢谢

This is a sample of my form. when the page loads the focus must be on 'fbox' but it dosent work and i don understand why. the form contains a niceditor but i dont think that is the problem

<html>
<head></head>
<body onload="document.form.fbox.focus();">
<body>
<form method='post' action='' name='form' >
Headline <input name='fbox' type='text' class='form' id='box' autocomplete='off' size='80'><br>
Your text</font><br><script type="text/javascript" src="nicEdit.js"></script><script type="text/javascript">bkLib.onDomLoaded(function() { nicEditors.allTextAreas() });</script>
<textarea name="description" style="width: 100%; height:200px;"></textarea></p>     
<p><select name='catg' >
<option value='' selected >Select category</option>
</select>
<input type="submit" id='button' name="Submit" value="Submit" class="button"></form>
</body></html>

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2024-11-07 23:30:52

您有两个正文标签。我建议摆脱其中一个,看看是否有帮助。

该代码中间还有一个杂散的结束 标记。许多人尝试排列标记,以便于阅读和查看文档的结构。您可能想探索这种做法。

另一种可能性是您的“nicEdit”插件正在取消“.focus()”调用。尝试将其取出并查看焦点是否有效(作为实验)。如果发生这种情况,那么您可以在 nicEdit 代码完成后执行“focus()”调用:(

bkLib.onDomLoaded(function() {
  nicEditors.allTextAreas();
  document.form.fbox.focus();
});

这是对现有

You've got two body tags. I suggest getting rid of one of them and seeing if that helps.

There's also a stray closing </font> tag in the middle of that code. Many people try to arrange their markup so that it's easy to read and to see the structure of the document. You might want to explore that practice.

Another possibility is that your "nicEdit" plugin is un-doing the ".focus()" call. Try taking that out and seeing if the focus works (as an experiment). If that's happening, then you can do your "focus()" call after the nicEdit code has finished:

bkLib.onDomLoaded(function() {
  nicEditors.allTextAreas();
  document.form.fbox.focus();
});

(That's an adaptation of the code in your existing <script> block.)

风向决定发型 2024-11-07 23:30:52

你有两个身体元素。那是行不通的。

脚本从上到下运行,因此您可以在页面的其余部分呈现之前触发“onload”。当事件触发时,您尝试关注的元素可能不存在。

有很多方法可以解决这个问题。 jQuery 有一个方便的方法,可以等待 DOM 准备好。

一种简单的方法是从脚本块中的页面底部运行脚本。

<script type="text/javascript">
document.form.fbox.focus()
</script>

You have two body elements. That won't work.

Scripts run top-to-bottom, so you're firing the "onload" before the rest of the page has rendered. It's likely the element you are trying to focus is not there when the event fires.

There are a number of ways to fix this. jQuery has a handy method that waits until the DOM is ready.

A simple way is simply to run your script from the bottom of the page in a script block.

<script type="text/javascript">
document.form.fbox.focus()
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文