为什么 onload 焦点在此脚本上不起作用?
这是我的表格样本。当页面加载时,焦点必须位于“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有两个正文标签。我建议摆脱其中一个,看看是否有帮助。
该代码中间还有一个杂散的结束
标记。许多人尝试排列标记,以便于阅读和查看文档的结构。您可能想探索这种做法。
另一种可能性是您的“nicEdit”插件正在取消“.focus()”调用。尝试将其取出并查看焦点是否有效(作为实验)。如果发生这种情况,那么您可以在 nicEdit 代码完成后执行“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:
(That's an adaptation of the code in your existing
<script>
block.)你有两个身体元素。那是行不通的。
脚本从上到下运行,因此您可以在页面的其余部分呈现之前触发“onload”。当事件触发时,您尝试关注的元素可能不存在。
有很多方法可以解决这个问题。 jQuery 有一个方便的方法,可以等待 DOM 准备好。
一种简单的方法是从脚本块中的页面底部运行脚本。
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.