我可以调用 $(document).ready() 来重新激活所有加载事件处理程序吗?
有谁知道我是否以及如何重新调用所有加载事件处理程序?我引用了一些我无法控制的 .js 文件,这些 .js 库在 $(document).ready() 中进行初始化,不幸的是没有提供任何简单的函数来重新初始化。
我目前正在尝试用 ajax 调用中的内容替换一个大的 div 块,因此我必须重新初始化外部库。因此,最好调用 $(document).ready() 来重新初始化所有内容。
到目前为止,我已经在 ajax 调用上尝试过此操作:
success: function(data) {
alert('1'); // Displays '1'
$('#content').html(data);
alert('2'); // Displays '2'
$(document).ready();
alert('3'); // Does not display
}
调用 $(document).ready();
也会悄悄失败。 JavaScript 控制台没有显示任何错误。有谁知道这是否可能(不修改javascript库文件)?
Does anyone happen to know IF and HOW I could re-call all on-load event handlers? I'm referencing some .js files that I DON'T have control over, and these .js libraries do their initialization in $(document).ready(), and unfortunately don't provide any easy function to re-initialize.
I'm currently trying to replace a large div block with content from an ajax call, and so I have to re-initialize the external libraries. So, it would be nice just to call $(document).ready() in order to re-initialize EVERYTHING.
So far, I've tried this on the ajax call:
success: function(data) {
alert('1'); // Displays '1'
$('#content').html(data);
alert('2'); // Displays '2'
$(document).ready();
alert('3'); // Does not display
}
Calling $(document).ready();
fails quietly too. JavaScript console shows no errors. Does anyone know if this is possible (without modifying javascript library files)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
既然你问如何在不修改外部JS文件的情况下做到这一点,我就这样回答。我已经在调试器中跟踪了 jQuery 中的
.ready()
函数,看起来页面准备好时调用的根函数是这样的:但是,看来你不能只调用它再次完成你想要的,因为看起来当它第一次触发时,它与之前注册的函数解除绑定(例如忘记它们)。因此,第二次手动调用 jQuery.ready() 不会再次重新触发相同的函数调用,我在调试器中验证了这一点(断点仅被命中一次,而不是第二次)。
因此,如果不更改 jQuery 实现,使其不会取消绑定(以允许多次触发),或者更改每段就绪处理程序代码以使用您自己的事件(您可以多次触发),则似乎无法解决此问题想。
Since you asked how to do it without modifying the external JS files, I'll answer that way. I've traced through the
.ready()
function in jQuery in the debugger and it appears that the root function that gets called when the page is ready is this:But, it appears you cannot just call it again to accomplish what you want because it appears that when it fires the first time, it unbinds from the functions that were previously registered (e.g. forgetting them). As such, calling
jQuery.ready()
manually a second time does not retrigger the same function calls again and I verified that in the debugger (breakpoint was only hit once, not second time).So, it appears that you cannot solve this problem without either changing the jQuery implementation so it doesn't unbind (to allow multiple firings) or changing each piece of ready handler code to use your own events that you can fire as many times as you want.
我做了类似的事情:
现在我可以在需要时随时调用这个函数。
I did something like:
Now I can call this function anytime I need.
实现此目的的一个简单方法就是创建自己的事件,如下所示:
然后添加此:
最后,每当您需要再次运行它时,只需调用此:
[编辑]
如果您确实无法编辑外部脚本文件我制作了一个 jsFiddle ,可以让您想做的事情成为可能,您可以在此处查看代码: http://jsfiddle.net/5dRxh/
但是,如果您不想使用它,那么在包含 jQuery 之后立即添加此脚本非常重要,如下所示:
A simple way to achieve this is just to invent your own event like this:
Then add this:
And last, whenever you need to run it again you simply call this:
[Edit]
If you really can't edit the external script-files I've made a jsFiddle that makes what you want to do possible, you can take a look at the code here: http://jsfiddle.net/5dRxh/
However, if you wan't to use this, it's important that you add this script RIGHT AFTER you include jQuery, like this:
如果更改整个正文内容,则可以第二次触发 document.ready:
You can trigger document.ready second time if you change entire body content:
我不认为这是可以做到的,因为 jquery 在执行后解除了就绪事件的绑定。从源头看:
I don't think that this can be done since jquery unbinds the ready event after it is executed. From the source:
你可以做这个简单的事情。
创建一个函数:
将 Reinit() 函数放入 doc.ready 中:
然后在 ajax 操作之后调用
You can do this simple.
Make a function:
Place just the Reinit() function inside doc.ready:
then after an ajax action just call
我认为直接将就绪事件更改为 pjax success
将其从: 更改
为:
I think it is straight forward to just change the ready event to pjax success
Change it from:
To:
这将是您想要的,只需保留就绪事件,直到您真正准备好为止。
https://api.jquery.com/jquery.holdready/
This will be what you want, just hold the ready event until you are really ready.
https://api.jquery.com/jquery.holdready/
或者,尝试这个:
而不是通过说来定义函数:
说:
解释:
加载到“document_ready”的任何函数都将自动加载到“$(document).ready ()”和“$(document).ajaxComplete ( )”并且在两种情况下都会触发。
Or, try this:
And instead of defining a function by saying:
say:
Explanation:
Any function loaded to "document_ready" will be automatically loaded into both "$(document).ready ()" and "$(document).ajaxComplete ()" and will fire under both circumstances.
我刚刚遇到的问题是,我的
ajax
代码仅在被$(document).ready(function(){}); 调用时才起作用; 而不是在常规函数中,所以我无法包装它。
该代码是关于加载页面的一部分的,由于一些加载错误,我希望在
超时
后再次调用它。我发现代码不必位于
$(document).ready(function(){});
中,但可以由它运行并且可以也可以自行调用。因此,在我从不同页面阅读了许多解决方案后,现在我将以下代码混合在一起:
I just had the problem that my
ajax
code only worked if it gets called by the$(document).ready(function(){});
and not in a regular function, so I couldn't wrap it.The code was about loading a part of my page and because of some loading errors I wanted it to be called again after a
timeout
.I found out that the code doesn't have to be in the
$(document).ready(function(){});
but can be run by it and can also be called by itself.So after I read many solutions from different pages now I've got this code mixed together: