简体   繁体   English

$ {document).ready不能替换在页面底部声明的js,但是我需要在脚本顶部

[英]$(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.

相关问题 我可以在页面上声明jQuery的文档就绪函数多少次? - How many times can I have jQuery's document ready function declared on a page? $(文件)。就绪(函数(){}); vs页面底部的脚本 - $(document).ready(function(){}); vs script at the bottom of page jQuery:如果在页面底部使用外部JS,为什么要使用document.ready? - jQuery: Why use document.ready if external JS at bottom of page? 我可以将脚本放在网页的底部,但可以从顶部调用吗? - Can I put a script at the bottom of a web page, but call it from the top? <script> tag at bottom causing $(document).ready to fail - <script> tag at bottom causing $(document).ready to fail 我的切换器不工作。 我试过将 js cdn 放在顶部和底部,但没有用 - My toggler is not working. I have tried placing js cdn in top and bottom both but didn't work 如何从中心而不是顶部准备/启动页面? - How can I ready/start the page from the center instead of top? 如何有效地使用yepnope.js和$(document).ready()? - How can I use yepnope.js with $(document).ready() effectively? 在页面上我声明了一个js变量,include js文件似乎无法访问它? - On page I declared a js variable, include js file can't seem to access it? 如何让JS脚本为每个查看它的人更新页面? - How can I have a JS script update a page for everyone viewing it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM