Javascript 什么 jQuery 事件在 $(document).ready() 之后被调用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8910637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:03:11  来源:igfitidea点击:

What jQuery event is called right after $(document).ready()?

javascriptjqueryhtmldom

提问by myWallJSON

I have lots of HTML generated on $(document).ready(). I have a simple window system. But not only it is generated on $(document).ready()- also some HTML elements (different JS files put stuff into $(document).ready()). I want my window system to be generated after $(document).ready()is called. So how do I handle a function to be called after all the code registered in $(document).ready()is completed?

我在$(document).ready(). 我有一个简单的窗口系统。但不仅它是生成的$(document).ready()- 还有一些 HTML 元素(不同的 JS 文件将内容放入$(document).ready())。我希望我的窗口系统在$(document).ready()被调用后生成。那么在所有注册的代码$(document).ready()完成后如何处理要调用的函数呢?

回答by Dmitriy Yusupov

  $(window).load(function(){
   //some code after ready 
  });

回答by timing

There is another event which is fired later. it's $(window).load(); This is fired after all resources are loaded.

还有另一个事件稍后被触发。它是 $(window).load(); 这是在加载所有资源后触发的。

But perhaps you want this:

但也许你想要这个:

function loadWindowSystem(){
    // load window system here
}

$(document).ready(function(){
    // do some html stuff here

    loadWindowSystem();
})

This way you can separate your code in functions.

这样你就可以在函数中分离你的代码。

回答by Mac Attack

I usually don't advocate using setTimeout, but you can build on top of @jfriend00's answer to create a more abstract approach:

我通常不提倡使用setTimeout,但您可以在@jfriend00 的回答基础上创建更抽象的方法:

$(document).ready(function() {
    setTimeout(function() {
        $(document).trigger('afterready');
    }, 1);
});

$(document).bind('afterready', function() {
    // call your code here that you want to run after all $(document).ready() calls have run
});

回答by jfriend00

If you want something to fire right after all $(document).ready()calls, you can put this once anywhere in your page:

如果你想在所有$(document).ready()调用之后立即触发某些东西,你可以把它放在你页面的任何地方:

$(document).ready(function() {
    setTimeout(function() {
        // call your code here that you want to run after all $(document).ready() calls have run
    }, 1);
});

This will get called along with all the other document.ready calls, but it sets a short timeout that will execute after all the other document.ready calls have finished.

这将与所有其他 document.ready 调用一起被调用,但它会设置一个短暂的超时时间,该超时将在所有其他 document.ready 调用完成后执行。

回答by Jessy Czaplicki

By adding another handler function ones the document is ready, the handler will almost certainly be last one in the ready event queue. Effectively giving you an instant "post" ready handler function.

通过添加另一个处理程序函数,文档准备就绪,处理程序几乎肯定会是就绪事件队列中的最后一个。有效地为您提供即时的“发布”就绪处理程序功能。

$(document).ready(function() {
    $(document).ready(function() {
        // code to run "after" ready 
    });
});

$(document).ready

When multiple functions are added via successive calls to this method, they run when the DOM is ready in the order in which they are added.

$(document).ready

当通过连续调用此方法添加多个函数时,它们会在 DOM 准备就绪时按添加顺序运行。

Note that this will only work as long as no ones else uses this "trick".

请注意,这仅在没有其他人使用此“技巧”时才有效。

回答by SergeS

there is nothing after this function , so if you have some ajax loaders, only think you can do is to wait for all of them and then start rendering

这个函数之后没有任何东西,所以如果你有一些ajax加载器,你只能认为你能做的就是等待所有它们然后开始渲染

EDIT But i wonder why you dont just structuralize your code to eliminate this.

编辑但我想知道为什么你不只是结构化你的代码来消除这个。

回答by DevCoDesign

$(document).ready()is called just after the DOM has finished loading. pageLoad()is called next on a 0 timer, but beware it is run after every partial postback.

$(document).ready()在 DOM 完成加载后立即调用。 pageLoad()在 0 计时器上调用 next,但要注意它在每次部分回发后运行。

Edit: Added side note - this will only count if using ASP.NET, the pageLoad functionality mentioned is separate from jQuery. See more info Here

编辑:添加旁注 - 这只会在使用 ASP.NET 时计算,所提到的 pageLoad 功能与 jQuery 是分开的。在这里查看更多信息