在哪里放置$(文件)。就绪(函数()? [英] Where to place $(document).ready(function()?

查看:182
本文介绍了在哪里放置$(文件)。就绪(函数()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们经常在这里和那里阅读,我们必须把我们的js code无论是在页面头部或的的(对不起)结束body标签。这个一旁的讨论,我只是看看就知道了浏览器是什么这样的事情的阅读顺序(以他们做作为这里等于):

We often read here and there, that we must place our js code either on the page head section or before (sorry) the end body tag. Discussions about this aside, I'm just looking to know what are the reading order for such things by the browsers (taking that they do act as equals here):

我们可以把:

$(document).ready(function(){

无论身在何处的页面结构,因为我们使用 $(文件)。就绪还是应该我们还是把它放在头部分?

No matter where on the page structure, because we are using $(document).ready or should we still place it on the head section ?

任何人都可以请澄清这一点。

Can anyone please clarify this.

如果我的问题不明确,我可以改一下。

If my question isn't clear, I can rephrase.

推荐答案

您可以在任何地方放置 脚本在文档中。最佳做法通常是将通知:脚本在页脚的页面加载性能的担忧。此外,最好的做法通常是将通知:脚本共同为便于维修。

You can place a script anywhere in the document. Best practice usually advices placing scripts in the footer for page load performance concerns. Further, best practice usually advices placing the scripts together for ease of maintenance.

不过,按照规范,对在文档中放置一个剧本标签没有限制。你可以把它们一起在报头,在主体的底部,洒遍布文件,或它们的任意组合。

However, per the spec, there is no restriction on where in the document you place a script tag. You may place them together in the header, at the bottom of the body, sprinkled all over the document, or any combination thereof.

使用了jQuery的构建 $(文件)。就绪有相同的结果,无论它被放置在文档中的。关键是要理解这种结构背后的功能:

The use of the jQuery construct $(document).ready has the same result regardless of where it is placed within the document. The key is to understand the functionality behind this construct:

虽然JavaScript提供负载事件当一个页面呈现,不会被触发此事件,直到如图像的所有资产已经被完全接收。执行code

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received.

因此​​,就绪类似于 document.onload ,但不一样的。没关系,其中code是,如果执行它时, document.onload 被解雇或当jQuery的火灾就绪。如果不是由某些事件处理函数/侦听器包裹code的文档中的位置是唯一显著。

So, ready is similar to document.onload, but not the same. It doesn't matter where the code is, if you execute it when document.onload is fired or when jQuery fires ready. Code's placement in a document is only significant if it is NOT wrapped by some event handler/listener.

$(文件)。就绪上的位置的唯一限制是您包括jQuery库之前,它不会发生。 $(文件)。就绪正在使用jQuery,所以如果jQuery的不存在....你不能使用它。

The only restriction on the location on $(document).ready is that it cannot happen before you include the jQuery library. $(document).ready is using jQuery, so if jQuery doesn't exist.... you can't use it.

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script>
            alert('executed as soon as it is reached (as the document is downloaded)');
            $(document).ready(function () { alert('on jQuery ready'); });
        </script>
    </head>
    <body onload="alert('executed on document.onload event');">
        <script>
            alert('executed as soon as it is reached (as the document is downloaded)');
            $(document).ready(function () { alert('on jQuery ready'); });
        </script>
    </body>
</html>

文档


  • SCRIPT规范在W3 - <一个href=\"http://www.w3.org/TR/html401/interact/scripts.html\">http://www.w3.org/TR/html401/interact/scripts.html

  • 脚本(HTML 5)规范的W3 - <一个href=\"http://www.w3.org/TR/html-markup/script.html\">http://www.w3.org/TR/html-markup/script.html

  • 在怪异模式配售中的JavaScript功能页的 - http://www.quirksmode.org/ JS / placejs.html

  • Jquery的就绪 - http://api.jquery.com/ready/

  • SCRIPT specification at W3 - http://www.w3.org/TR/html401/interact/scripts.html
  • script (html 5) specification at W3 - http://www.w3.org/TR/html-markup/script.html
  • Placing Javascript in your pages at quirksmode - http://www.quirksmode.org/js/placejs.html
  • Jquery ready - http://api.jquery.com/ready/

这篇关于在哪里放置$(文件)。就绪(函数()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆