gpt4 book ai didi

jquery - jquery 中文档就绪到底意味着什么

转载 作者:行者123 更新时间:2023-12-01 00:45:45 25 4
gpt4 key购买 nike

假设我有一个包含 JavaScript 文件的 HTML 页面:

base.js 是这样的:

$(document).ready(function () {
obj.init();
}

// ..............

var obj = {...};

令人惊讶的是,有时(并非总是)Firebug 在 obj.init() 调用上向我显示未定义的错误!我的理解是,文档就绪意味着所有html元素,包括图像、下载并执行的javascript文件(?)。

我相信为了找到这个错误的根本原因,我们需要了解“文档准备好”到底意味着什么?有人有任何见解吗?

==============================

更新:也许我不应该在这里提及图像,我主要关心的是 JavaScript 文件。 “完全构建的 DOM”是否包括“执行的所有 javascript 代码”?

==============================

再次更新:似乎这里的人都同意,在所有 javascript 代码下载并执行之前,不会触发事件“document.ready”。因此,问题的根本原因仍然未知。在将 $(document).ready block 移动到 javascript 文件的底部后,我确实绕过了这个问题。

最佳答案

来自 jQuery.ready() 的文档:

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

关于jquery - jquery 中文档就绪到底意味着什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9504315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com