如何在 jQuery 中触发 $().ready() ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/562229/
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
How to trigger $().ready() in jQuery?
提问by Aaron Lee
Using jQuery, is there a way to retrigger the document.ready
trigger at some point after a page has loaded?
使用 jQuery,有没有办法document.ready
在页面加载后的某个时刻重新触发触发器?
Update:
jQuery sheds them once they are run. In my experiments it looks like jQuery will run a ().ready
callback as soon as it is encountered once the initial ready
event is triggered.
更新:jQuery 会在它们运行后删除它们。在我的实验中,().ready
一旦ready
触发初始事件,jQuery 就会在遇到回调时立即运行。
回答by Matthew Crumley
If you just need to make sure that new initialization functions get run, you don't need to do anything. When you add a new event handler in $().ready
, after the document finishes loading, it will run immediately.
如果您只需要确保运行新的初始化函数,则无需执行任何操作。当您在 中添加新的事件处理程序时$().ready
,文档加载完成后,它将立即运行。
Apparently, calling $().trigger("ready")
directly doesn't work because jQuery resets the ready event handlers after it runs them. You will need to keep track of the functions you need to call, and call them directly as below.
显然,$().trigger("ready")
直接调用是行不通的,因为 jQuery 会在运行就绪事件处理程序后重置它们。您需要跟踪需要调用的函数,并直接调用它们,如下所示。
Of course, it might be better to just call the function directly, so you don't re-run something you didn't intend to (some plugins might have their own $().ready() functions that they don't expect to run twice).
当然,直接调用函数可能会更好,这样你就不会重新运行你不想要的东西(一些插件可能有自己的 $().ready() 函数,它们不希望运行两次)。
$().ready(initializationFunction);
// later:
initializationFunction(jQuery);
回答by kzh
You can do something like this:
你可以这样做:
$(document).on('ready readyAgain', function() {
// do stuff
});
// At some other point in time, just trigger readyAgain
$(document).trigger('readyAgain');
Note:
笔记:
$(document).on('ready')
has been deprecated in jQuery 1.8 according to documentation. An alternative method is as follows:
$(document).on('ready')
根据文档,已在 jQuery 1.8 中弃用。另一种方法如下:
function myReadyFunction(){}
$(myReadyFunction);
// at some other point, just call your function whenever needed:
myReadyFunction($);
回答by Miguel ángel Pérez
What if you tried building a module execution controller right into the ready function? Kind of like this:
如果您尝试将模块执行控制器直接构建到就绪函数中会怎样?有点像这样:
回答by Roy Shoa
In some cases you need to add more code (scripts) in another places of the code, so creating a function limit you to write all the code in the same area. This why I prefer to add my custom events, and then trigger them:
在某些情况下,您需要在代码的其他位置添加更多代码(脚本),因此创建函数限制您在同一区域编写所有代码。这就是为什么我更喜欢添加我的自定义事件,然后触发它们:
//Add a page Ready custom event
$( document ).on( "page.ready", function( event ) {
//Your Ready Scripts
});
//Add a page Load custom event
$( document ).on( "page.load", function( event ) {
//Your Load Scripts
});
//Trigger Ready scripts on page ready
jQuery(function($) {
$( document ).trigger( "page.ready" );
});
//Trigger Load scripts on page load
$(window).load(function () {
$( document ).trigger( "page.load" );
});
//Trigger the Custom events manually
$( document ).trigger( "page.ready" );
$( document ).trigger( "page.load" );
https://learn.jquery.com/events/introduction-to-custom-events/
https://learn.jquery.com/events/introduction-to-custom-events/
回答by RSolberg
Aaron - what would you be trying to accomplish, reinitializing variables?
Aaron - 你想完成什么,重新初始化变量?
Edited... The beauty of jQuery in my opinion is the call back functions. Why not just have some action/function executed after the event.
编辑... 在我看来,jQuery 的美妙之处在于回调函数。为什么不在事件之后执行一些动作/功能。
回答by Steadicat
I can see one scenario where this would be very useful, that is if you delay loading jQuery until everything else has loaded, then you dynamically append a new script element to load jQuery.
我可以看到一种非常有用的场景,即如果您延迟加载 jQuery,直到其他所有内容都加载完毕,然后您动态地附加一个新的脚本元素来加载 jQuery。
In this case, $(document).ready is not always triggered, since in many cases the document will have completely loaded before jQuery is done loading and has set up the right event handlers.
在这种情况下,$(document).ready 并不总是被触发,因为在许多情况下,文档将在 jQuery 完成加载并设置正确的事件处理程序之前完全加载。
You could use named functions and call those instead, but it would still break any plugin that relies on $(document).ready.
您可以使用命名函数并调用它们,但它仍然会破坏任何依赖于 $(document).ready 的插件。
回答by user7303268
If it helps you:
如果它对您有帮助:
var document_on_ready = new Array();
function on_ready(){
for(i=0;i<document_on_ready.length;i++){
document_on_ready[i].call();
}
}
$(function(){
on_ready();
});
var counter = 0;
document_on_ready.push(function(){
counter++;
console.log('step1: ' + counter);
});
document_on_ready.push(function(){
counter++;
console.log('step2: ' + counter);
});
window.setTimeout(on_ready,1000);
回答by Denis
Easy: you just need to disable the safety-flag jQuery.isReady. Set jQuery.isReady to false and use jQuery(document).trigger('ready') to re-trigger this event. That's quite useful for turbolinks-style navigation.
简单:您只需要禁用安全标志 jQuery.isReady。将 jQuery.isReady 设置为 false 并使用 jQuery(document).trigger('ready') 重新触发此事件。这对于 turbolinks 风格的导航非常有用。
回答by Dan Davies Brackett
ASP.Net UpdatePanel partial page loads are another place where one might want to re-trigger loading functionality - not that $().ready
is the right place to do that. In that case, you can either use page_load or the MS framework's endrequest event.
ASP.Net UpdatePanel 部分页面加载是另一个可能想要重新触发加载功能的地方 - 不是这样$().ready
做的正确位置。在这种情况下,您可以使用 page_load 或 MS 框架的 endrequest 事件。
回答by user64640
You should be able to with document.ready.apply();
你应该能够使用 document.ready.apply();