重置 document.ready ($.getScript)
我需要通过 $.getScript 动态包含一个脚本。
然而问题是,动态包含的脚本中的 $(document).ready 函数没有被执行。 (它们动态插入不会在 dom 准备好时发生)
有什么方法可以强制它这样做吗?例如,通过伪造 document.ready 事件?
谢谢, 韦斯利
I need to include a script via $.getScript dynamically.
The problem is however, that the $(document).ready function from that dynamically included script is not executed. (They dynamic insertion does not happen at dom ready)
Is there some way to force it to do so? By, for example, faking a document.ready event?
Thanks,
Wesley
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
触发 doc Ready 后,任何将来的 doc Ready 调用都将在解析时执行。
阅读 API
After doc ready has been fired any future doc ready calls will be executed when parsed.
Read the API
通过
$.getScript()
定义的新处理程序缺少 document.ready 事件是有道理的,因为文档的 DOM 提前完成了加载。您是否尝试过
$(document).trigger('ready')
来重新触发该事件?当然,之前注册的所有事件处理程序都会被再次调用。It makes sense that new handlers defined through
$.getScript()
are missing the document.ready event since the document's DOM finished loading way earlier.Have you tried
$(document).trigger('ready')
in order to re-trigger that event? Of course, all event handlers registered earlier would be called again.在您的包含文件中,将初始化移出到另一个函数并从那里调用它:
在加载此文件的文件中,您所需要做的就是调用
initialise
触发就绪事件的主题已经很好地涵盖了SO:
如何在 jQuery 中触发 $().ready() ?
<一href="https://stackoverflow.com/questions/2238030/jquery-trigger-document-ready-so-ajax-code-i-cant-modify-is-execulated">触发 $document.ready (所以 AJAX 代码我无法修改执行)
In your include file, move the initialisation out to another function and call it from there:
This in the file that loads this all you need do is call
initialise
The subject of triggering the ready even is already well covered on SO:
How to trigger $().ready() in jQuery?
Trigger $document.ready (so AJAX code I can't modify is executed)