我可以调用$(document).ready()来重新激活所有on load事件处理程序吗? [英] Can I call $(document).ready() to re-activate all on load event handlers?

查看:211
本文介绍了我可以调用$(document).ready()来重新激活所有on load事件处理程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人碰巧知道IF以及我如何重新调用所有的on-load事件处理程序?我正在引用一些我无法控制的.js文件,这些.js库在$(document).ready()中进行初始化,遗憾的是,它们没有提供任何简单的函数来重新初始化。 / p>

我正在尝试用ajax调用中的内容替换大的div块,因此我必须重新初始化外部库。所以,只需调用$(document).ready()就可以重新初始化所有内容。



到目前为止,我已尝试过ajax call:

  success:function(data){
alert('1'); //显示'1'
$('#content')。html(data);
alert('2'); //显示'2'
$(文件).ready();
alert('3'); //不显示
}

调用 $(文档) .ready(); 也悄然失败。 JavaScript控制台显示没有错误。有没有人知道这是否可行(没有修改javascript库文件)?

解决方案

因为你问过怎么做而不修改外部JS文件,我会回答这个问题。我在调试器中通过jQuery中的 .ready()函数进行了跟踪,看来在页面准备就绪时调用的根函数是:

  jQuery.ready(); 

但是,看起来你不能只是再次调用它来完成你想要的东西,因为它似乎在它的时候它是第一次触发,它与之前注册的函数解除绑定(例如忘记它们)。因此,第二次手动调用 jQuery.ready()不再重新触发相同的函数调用,我在调试器中验证了(断点只被击中一次,而不是秒时间)。



所以,看来如果不更改jQuery实现就无法解决这个问题,因此它不会解除绑定(允许多次触发)或更改每个就绪处理程序代码使用您自己的事件,您可以根据需要多次触发。


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)?

解决方案

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:

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).

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.

这篇关于我可以调用$(document).ready()来重新激活所有on load事件处理程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆