[英]$(document).ready can't replace js declared at bottom of the page, but I need to have the script at the top
I have JQuery based tab navigation that only works if it is placed after the html for the menu. 我有基于JQuery的标签导航,只有将其放置在菜单的html之后,该标签导航才有效。 It works if it's placed at the end too, but breaks when I put the same script in $(document).ready ath the top.
如果它也放在末尾,它会起作用,但是当我将相同的脚本放在$(document).ready ath顶部时,它就会中断。
I thought that this (placed at the top of the page): 我认为这(位于页面顶部):
<script type="text/javascript">
$(document).ready(function(){
mouseovertabsmenu.init("mytabsmenu", "mysubmenuarea", true);
});
</script>
would be the same as this: 将与此相同:
<script type="text/javascript">
mouseovertabsmenu.init("mytabsmenu", "mysubmenuarea", true);
</script>
</html>
Which is placed at the bottom of the page and works. 它位于页面底部并可以使用。 How would I be able to put the function at the top or even include it from a separate file?
我如何能够将函数放在顶部,甚至可以将其包含在单独的文件中?
I had this problem and I had used this in the head after the jquery include link: 我有这个问题,并且在jquery include链接之后在头中使用过:
$(document).ready(myfunction());
This failed due to objects not being loaded. 由于未加载对象,此操作失败。 I should have wrapped my call in a function:
我应该将调用包装在一个函数中:
$(document).ready(function () {
myfunction();
});
This worked as expected only after the document was properly loaded. 仅在正确加载文档后,此功能才能按预期工作。
The reason it is recommended to place the script at the end of the page , is so that the HTML elements are loaded onto the page before you try to access them.. 建议将脚本放置在页面末尾的原因是,在尝试访问HTML元素之前,会将HTML元素加载到页面上。
No matter where you place the code it is better to wrap in DOM ready event which makes sure the complete HTML document is properly loaded before you try to access the elements in the page.. 无论将代码放置在何处,最好都包装在DOM ready事件中,该事件可以确保在尝试访问页面中的元素之前正确加载了完整的HTML文档。
Maybe in your case 也许在你的情况下
<script type="text/javascript">
mouseovertabsmenu.init("mytabsmenu", "mysubmenuarea", true);
</script>
seems to be working when not encased in DOM ready is the function is not accessible as it's scope is being blocked by DOM ready 未封装在DOM中时似乎工作正常,因为该函数的作用域被DOM准备中了,因此无法访问该函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.