Javascript 我可以调用 $(document).ready() 来重新激活所有加载事件处理程序吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7135752/
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
Can I call $(document).ready() to re-activate all on load event handlers?
提问by Mike Richards
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.
有没有人碰巧知道我是否可以重新调用所有加载事件处理程序?我引用了一些我无法控制的 .js 文件,这些 .js 库在 $(document).ready() 中进行初始化,不幸的是没有提供任何简单的函数来重新初始化。
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.
我目前正在尝试用来自 ajax 调用的内容替换一个大的 div 块,因此我必须重新初始化外部库。因此,最好只调用 $(document).ready() 以重新初始化一切。
So far, I've tried this on the ajax call:
到目前为止,我已经在 ajax 调用中尝试过这个:
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)?
呼叫也$(document).ready();
悄无声息地失败了。JavaScript 控制台没有显示错误。有谁知道这是否可行(不修改 javascript 库文件)?
回答by jfriend00
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:
既然你问了如何在不修改外部JS文件的情况下做到这一点,我就这样回答。我已经.ready()
在调试器中跟踪了jQuery 中的函数,看起来页面准备好时调用的根函数是这样的:
jQuery.ready();
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).
但是,您似乎不能再次调用它来完成您想要的操作,因为当它第一次触发时,它会与先前注册的函数解除绑定(例如忘记它们)。因此,jQuery.ready()
第二次手动调用不会再次重新触发相同的函数调用,我在调试器中验证了这一点(断点只被击中一次,而不是第二次)。
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.
因此,如果不更改 jQuery 实现以便它不会解除绑定(以允许多次触发)或更改每段就绪处理程序代码以使用您自己的事件,则您似乎无法解决此问题,您可以多次触发这些事件想。
回答by Fabio Nolasco
I did something like:
我做了类似的事情:
// When document is ready...
$(function(){
onPageLoad();
});
function onPageLoad(){
// All commands here
}
Now I can call this function anytime I need.
现在我可以随时调用这个函数。
回答by Alxandr
A simple way to achieve this is just to invent your own event like this:
实现这一目标的一个简单方法就是像这样发明自己的事件:
$(document).bind('_page_ready', function() { /* do your stuff here */});
Then add this:
然后添加这个:
$(function() { $(document).fire('_page_ready'); }); // shorthand for document.ready
And last, whenever you need to run it again you simply call this:
最后,每当您需要再次运行它时,您只需调用它:
$(document).fire('_page_ready');
[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/
如果您真的无法编辑外部脚本文件,我制作了一个 jsFiddle 使您想要做的事情成为可能,您可以在此处查看代码: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:
但是,如果您不想使用它,请务必在包含 jQuery 后立即添加此脚本,如下所示:
<script src="jquery.js" type="text/javascript"></script>
<script>
//script from jsFiddle (only the plugin part at the top).
</script>
<!-- All the other script-files you want to include. -->
回答by natedavisolds
I don't think that this can be done since jquery unbinds the ready event after it is executed. From the source:
我不认为这可以完成,因为 jquery 在执行就绪事件后解除绑定。从来源:
// Trigger any bound ready events
if ( jQuery.fn.trigger ) {
jQuery( document ).trigger( "ready" ).unbind( "ready" );
}
回答by zoberg
You can trigger document.ready second time if you change entire body content:
如果您更改整个正文内容,您可以第二次触发 document.ready:
$('body').html($('body').html())
回答by catalin87
You can do this simple.
你可以简单地做到这一点。
Make a function:
做一个函数:
function REinit() {
/// PLACE HERE ALL YOUR DOC.READY SCRIPTS
}
Place just the Reinit() function inside doc.ready:
将 Reinit() 函数放在 doc.ready 中:
$(document).ready(function(){
REinit();
});
then after an ajax action just call
然后在 ajax 操作之后调用
REinit();
回答by Wazery
I think it is straight forward to just change the ready event to pjax success
我认为将就绪事件更改为 pjax 成功是直接的
Change it from:
将其更改为:
$(document).ready(function() {
// page load stuff
});
To:
到:
$(document).on('ready pjax:success', function() {
// will fire on initial page load, and subsequent PJAX page loads
});
回答by Lomax
This will be what you want, just hold the ready event until you are really ready.
这将是您想要的,只需保持就绪事件,直到您真正准备好。
回答by Rex the Strange
Or, try this:
或者,试试这个:
jQuery.extend ({
document_ready: function (value) {
$(document).ready (value);
$(document).ajaxComplete (value);
}/* document_ready */
});
And instead of defining a function by saying:
而不是通过说来定义一个函数:
$(document).ready (function () { blah blah blah });
say:
说:
jQuery.document_ready (function () { blah blah blah });
Explanation:
解释:
Any function loaded to "document_ready" will be automatically loaded into both "$(document).ready ()" and "$(document).ajaxComplete ()" and will fire under both circumstances.
加载到“document_ready”的任何函数都将自动加载到“$(document).ready()”和“$(document).ajaxComplete()”,并且在这两种情况下都会触发。
回答by S.Witch
I just had the problem that my ajax
code onlyworked 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 errorsI wanted it to be called again after a timeout
.
我只是遇到了一个问题,我的ajax
代码只有在被 调用$(document).ready(function(){});
而不是在常规函数中才起作用,所以我无法包装它。
代码是关于加载我页面的一部分,由于一些加载错误,我希望它在timeout
.
I found out that the code doesn't have to be inthe $(document).ready(function(){});
but can be run by it and can also be called by itself.
我发现代码不必在 中,$(document).ready(function(){});
但可以由它运行,也可以由它自己调用。
So after I read many solutions from different pages now I've got this code mixed together:
因此,在我阅读了来自不同页面的许多解决方案之后,现在我将这段代码混合在一起:
$(document).ready(loadStuff);
function loadStuff(){
$.ajax({
type: "POST",
url: "path/to/ajax.php",
data: { some: data, action: "setContent"},
timeout: 1000, //only one second, for a short loading time
error: function(){
console.log("An error occured. The div will reload.");
loadStuff();
},
success: function(){
$("#divid").load("path/to/template.php"); //div gets filled with template
}
});
}