简体   繁体   English

如果代码位于底部,那么放置$(document).ready函数有什么意义呢?

[英]What is the point of putting the $(document).ready function if the code is at the bottom?

Fairly new to Jquery here.... but one thing I have been told and being doing, is add my Javascript at the bottom of my page after the html is read. 这里对Jquery来说相当新......但是我被告知和正在做的一件事是在阅读html之后在我的页面底部添加我的Javascript。

Now, I see people adding the $(document).ready(function() even when the code is at the bottom of the page. Isn't the DOM being progressively built as the HTML is read? By the end of reading the HTML, shouldn't the DOM be automatically be ready and therefore, what is the point of adding this check? 现在,我看到人们添加了$(document).ready(function(),即使代码位于页面底部。在读取HTML时是不是逐步构建DOM?在阅读HTML结束时,不应该自动准备DOM,因此,添加此检查的重点是什么?

For example, small demo: 例如,小型演示:

<ul>
    <li id="draggable" class="ui-state-highlight">Drag me down</li>
</ul>

<ul id="sortable">
    <li class="ui-state-default">Item 1</li>
    <li class="ui-state-default">Item 2</li>
    <li class="ui-state-default">Item 3</li>
    <li class="ui-state-default">Item 4</li>
    <li class="ui-state-default">Item 5</li>
</ul>
<script>
alert("In Page");
</script>

</div><!-- End demo -->

<script>
$(function() {
    alert("Dom is READY");
    $( "#sortable" ).sortable({
    revert: true
    });

    $( "#accordion" ).accordion();
});
</script>

The "In Page" always appear first... is it because the HTML is not "big" enough? “In Page”总是首先出现......是因为HTML不够“大”吗?

Truth is that document.ready and bottom of document are pretty much the same thing, since by the end of document all controls are there. 真相是document.ready和文档底部几乎是一回事,因为在文档末尾所有控件都存在。 Personally I will still prefer document.ready since it's the way JQuery framework identifies document end (and ideally we should stick to framework's recommended ways) and secondly it will take care of anyone moving the code by mistake. 我个人仍然会更喜欢document.ready,因为它是JQuery框架识别文档结束的方式(理想情况下我们应该坚持框架的推荐方式),其次它会照顾任何人错误地移动代码。

When you write your code inline that way, assuming you load jQuery in the head, having the document onReady may not be necessary. 当你以这种方式内联编写代码时,假设你在头部加载jQuery,可能没有必要使用文档onReady。

It starts to make a difference when your page code is loaded via an external JavaScript resource in the document (which may not be at the bottom). 当您的页面代码通过文档中的外部JavaScript资源(可能不在底部)加载时,它开始有所不同。 Reasons to do so are mainly so that the code can be cached by your browser and so reduces network overhead. 这样做的原因主要是因为您的浏览器可以缓存代码,从而减少网络开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 $(文件)。就绪(函数(){}); vs页面底部的脚本 - $(document).ready(function(){}); vs script at the bottom of page $(document).bind(&#39;ready&#39;,function)和$(document).ready(function(){})之间有什么区别 - What are the difference between $(document).bind('ready', function) and $(document).ready(function() {}) 如何在 document.ready 上调用多个 js 函数而不放置它们 jQuery(document).ready(function(){}); - How to call multiple js functions on document.ready without putting them jQuery(document).ready(function(){}); $(document).ready(function()和$(function())有什么区别? - What is the difference between $(document).ready(function() and $(function() ? 文件就绪功能-不先加载代码 - Document ready function - Not loading the code first jQuery:在页面代码和document.ready代码之间共享功能 - jQuery: sharing function between page code and document.ready code 如果我使用 jQuery(document).ready(function(){ ... my code}) = undifined function - If i use jQuery(document).ready(function(){ … my code}) = undifined function $(document).ready 上的函数不起作用 - function on $(document).ready not working $(document).ready 函数不会触发 - $(document).ready function will not fire 带有$(document).ready(function()的Javascript - Javascript with $(document).ready(function()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM