问题描述
我有一个生成HTML块的JavaScript。此脚本有时在运行时调用,有时在加载文档之前调用。我想要一个能够判断文档是否准备就绪的脚本。如果是,则生成HTML,否则,添加document.ready()函数。什么是jQuery知道文档是否已被加载的最佳方式?
I have a JavaScript that generates HTML blocks. This script is sometimes called somewhere in run time, and sometimes before document is loaded. I want a script that is able to tell if document is ready. If so, generate the HTML, otherwise, add a document.ready() function. What is jQuery's best way to know if document has been loaded?
推荐答案
始终将该HTML生成代码包装在$中是安全的(文件)。就绪()。如果文档已经准备就绪,则会立即执行新注册的$(document).ready()回调。
It's safe to always wrap that HTML generation code in $(document).ready(). If the document is already ready, a newly registered $(document).ready() callback will execute immediately.
这篇关于你怎么知道文件是否准备好了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!