jQuery的ready()方法的快捷方式

发布于 2024-09-26 19:56:45 字数 331 浏览 4 评论 0原文

我已经看到了ready()方法的一些快捷方式,并且想知道哪个实际上首先发生,因为我的测试结果让我感到困惑。

$(document).ready(function(){
    alert("document ready");
});

$(window).load(function(){
    alert("window ready");
});

(function($){
    alert("self invoke");
})(jQuery);

这里首先发生自调用,然后是文档,然后是窗口。 自调用技术是否被视为ready()方法?

I have seen some shortcuts for the ready() method and would like to know which actually happens first, because my test results confuse me..

$(document).ready(function(){
    alert("document ready");
});

$(window).load(function(){
    alert("window ready");
});

(function($){
    alert("self invoke");
})(jQuery);

Here self invoke happens first, then document, then window. Is the self invoke technique considered a ready() method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毅然前行 2024-10-03 19:56:45

第三个选项不是 .ready() (或实际上与 jQuery 相关)的快捷方式,自调用立即运行(一旦出现在代码中),< em>这可能是您正在考虑的快捷方式:

$(function(){
  alert("I'm a ready shortcut");
});

将函数传递到 $(func)$(document).ready(func); 的快捷方式。 无冲突 版本如下所示:

jQuery(function($) {
  //$ is jQuery
});

The third option is not a shortcut for .ready() (or jQuery related really), the self invoke runs immediately (as soon as it appears in the code), this is probably the shortcut you're thinking of though:

$(function(){
  alert("I'm a ready shortcut");
});

Passing a function into $(func) is a shortcut for $(document).ready(func);. The no-conflict version would look like this:

jQuery(function($) {
  //$ is jQuery
});
梦里泪两行 2024-10-03 19:56:45

Nick Craver 的说法是正确的,但我认为值得注意的是,在最后一个示例中,它实际上根本没有对 jquery 执行任何操作。 jQuery 作为参数传递给匿名函数,但该函数没有对其执行任何操作。

最后一个示例相当于立即调用函数表达式 (IIFE):

(function(){
    alert("self invoke");
})();

显然,这只是在该行代码被命中后立即调用匿名函数,从而发出警报。它根本没有调用 jQuery,这就是为什么 Nick 说它绝对不是一个 read() 方法是正确的。

Nick Craver is right in what he says but I think it is worth noting that in that last example that it isn't actually doing anything with jquery at all. jQuery is being passed as a parameter to the anonymous function but the function isn't doing anything with it.

The last example is equivalent to an Immediately-Invoked Function Expression (IIFE):

(function(){
    alert("self invoke");
})();

And clearly this is just immediately calling the anonymous function as soon as that line of code is being hit and thus doing the alert. It isn't invoking jQuery at all which is why Nick is right when he says it is defintiely not a ready() method.

梦一生花开无言 2024-10-03 19:56:45

这篇文章很好地解释了前两者如何是不同的:

$(document).ready$(window).load

jQuery 提供了两种强大的方法来执行代码和附加事件
处理程序:$(document).ready$(window).load。文件准备好
当 HTML 文档加载且 DOM 已加载时,事件已执行
准备就绪,即使所有图形尚未加载。如果你想
在窗口加载之前连接某些元素的事件,然后
$(document).ready 是正确的地方。

$(文档).ready(function() {
  // 当 HTML 文档加载且 DOM 准备就绪时执行
  Alert("文档已准备好");
});

当完整的页面被加载时,窗口加载事件会稍后执行。
完全加载,包括所有帧、对象和图像。所以
应放置涉及图像或其他页面内容的功能
在窗口或内容标记本身的加载事件中。

$(窗口).load(function() {
  // 当整个页面完全加载时执行, 
  // 包括所有帧、对象和图像
  Alert("窗口已加载");
});

This article has a good explanation on how the first two are different:

$(document).ready vs. $(window).load

jQuery offers two powerful methods to execute code and attach event
handlers: $(document).ready and $(window).load. The document ready
event executes already when the HTML-Document is loaded and the DOM is
ready, even if all the graphics haven't loaded yet. If you want to
hook up your events for certain elements before the window loads, then
$(document).ready is the right place.

$(document).ready(function() {
  // executes when HTML-Document is loaded and DOM is ready
  alert("document is ready");
});

The window load event executes a bit later when the complete page is
fully loaded, including all frames, objects and images. Therefore
functions which concern images or other page contents should be placed
in the load event for the window or the content tag itself.

$(window).load(function() {
  // executes when complete page is fully loaded, 
  // including all frames, objects and images
  alert("window is loaded");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文