W3Schools 上另一个有问题的 jQuery 测验答案

发布于 2024-11-02 01:45:24 字数 1342 浏览 3 评论 0原文

W3Schools 网站上发布了一个 jQuery 测验...

http:// www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery

问题#16如下,

哪个 jQuery 函数用于在文档加载完成之前阻止代码运行?

A. $(文档).load()

B. $(文档).ready()

C. $(body).onload()

我选了答案 A,结果错了。(他们的官方答案是 B。)

我回答了,认为我知道以下内容,

  1. document.load 在页面上的所有内容加载包括所有图像后触发

  2. document.ready 仅在 DOM 加载后触发(不一定所有图像都已加载)

我解释了原始问题的措辞“文档已完成加载”,以包括所有内容(以及所有图像)。毕竟,我认为这就是他们称之为“document.load”的原因。此外,为了使他们的答案正确,您必须得出结论:“DOM”相当于“文档”。这看起来不对,否则为什么称其为“文档对象模型”(DOM)而不只是“文档”?

尽管 W3School 声称 B 是正确答案,但真正的正确答案是什么?

谢谢你的想法。

旁注:引用我自己在我的其他相关问题中的评论。

“实际上,我在 jQuery 网站上学习了大部分 jQuery,当我遇到困难时,我会在 StackOverflow 上进行搜索。我主要是在 W3School 上进行测验,因为我正在研究他们的 jQuery 认证的“先决条件”。我不认为自己是 jQuery 专家,但我在他们的测验中轻松获得了 95% (19/20),看到答案中的不当措辞,我想我应该通过在这里发布我的观点来确认我已经怀疑的内容。此类认证现在正在发生变化。”


编辑:

我已通知 W3Schools 该线程的存在。


编辑2:

当我回答原来的测验问题时,我想到的是$(window).load(),因此我的测验答案显然是不正确的。我相信这一事实使得三个多项选择选项中没有一个成为正确答案。请参阅下面我的详细回答。

There is a jQuery quiz posted on the W3Schools site here...

http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery

Question #16 is as follows,

Which jQuery function is used to prevent code from running, before the document is finished loading?

A. $(document).load()

B. $(document).ready()

C. $(body).onload()

I got it wrong by picking answer A. (their official answer is B.)

I answered it, thinking that I knew the following,

  1. document.load fires after everything on the page loads including all images

  2. document.ready fires only after the DOM is loaded (not necessarily all the images have been loaded)

I interpreted the original question's wording of, "document is finished loading", to include everything (with all the images). After all, I thought that's why they called it "document.load". Also for their answer to be correct, you'd have to conclude that "DOM" is the equivalent to "document". This does not seem right otherwise why call it "document object model" (DOM) instead of just "document"?

Despite the W3School claim that B is the correct answer, what is really the correct answer?

Thank-you for your thoughts.

Sidenote: Quoting my own comments in my other related question...

"I actually learn most of my jQuery over at the jQuery site and come search here at StackOverflow when I get stuck. I was mostly playing around with the quiz at W3School because I was investigating their "prerequisites" for jQuery Certification. I don't consider myself to be a jQuery expert but I easily scored 95% (19/20) on their quiz. Seeing the improper wording in that answer, I figured I'd confirm what I already suspected by posting here. My opinions on these kinds of certifications are now shifting."


EDIT:

I notified the W3Schools of the existence of this thread.


EDIT 2:

When I answered the original quiz question, I was thinking of $(window).load(), therefore my quiz answer was clearly incorrect. I believe this fact leaves none of the three multiple choice options as the correct answer. See my detailed answer below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰認得朕 2024-11-09 01:45:24

这个问题不明确的。

正确的答案取决于您对文档的定义。如果是 DOM,则为 B。如果是整个页面的资源,则为 $(window).load(function() { ... })

正如你所看到的,这个测验很糟糕。

The question is ambiguous.

The correct answer depends on your definition of document. If it is the DOM, it would be B. If it were the entire page's assets, it would be $(window).load(function() { ... }).

As you can see, this quiz sucks.

痴骨ら 2024-11-09 01:45:24

来自 jQuery API 文档...

.ready()

描述:指定一个函数
DOM 已满时执行
已加载。虽然 JavaScript 提供了
load 事件用于执行代码
页面已呈现,但此事件未呈现
被触发,直到所有资产,例如
图像已完全收到。
大多数情况下,脚本可以运行
一旦 DOM 层次结构完全构建完毕

有关 jQuery Ready 的更多信息

定义 DOM,来自 W3C...

什么是文档对象模型?

文档对象模型是
平台和语言中立
接口,将允许程序和
动态访问和的脚本
更新内容、结构和
文档的样式。

jQuery API 文档 定义 .load()

.load()

描述:将事件处理程序绑定到“load”JavaScript 事件。

示例:页面完全加载时运行函数,包括
图形。

$(窗口).load(函数(){

 // 运行代码 

});


经过研究和深思熟虑后,我得出的结论是,考虑到他们的三个选择,问题的措辞是不正确的。当然,基于这个思路,由于测验问题没有正确答案,所以我原来的答案也是错误的。

原始 W3Schools jQuery 测验问题:

使用哪个 jQuery 函数
在之前阻止代码运行
文档加载完毕?

现在我们来分析一下原来的三个答案:

A. $(文档).load()

这是我最初的答案,但在 StackOverflow 上发布这个问题后,我意识到 $(document).load() 不是有效的代码。 $(window).load() 是我最初的想法。使用 $(window).load(),您将阻止代码在整个窗口加载之前执行,包括所有元素、图像等。

A 不可能是正确的答案,因为措辞是“文档.load”虽然应该是“window.load”。

B. $(文档).ready()

这是官方答案,也是有效的 jQuery 代码。当 DOM 准备好但在其他任何内容完成加载之前它会触发。我认为你不能说“文档”已完成加载,否则“文档”和“DOM”一词将具有相同的含义并且可以互换。

B 不可能是正确答案,因为如果没有图像和其他资源,页面(文档)尚未完成加载。

C. $(body).onload()

C 不能仅仅因为“onload()”不是 jQuery 库的一部分而成为正确答案。

结论:

  1. 正如问题的措辞,“在文档加载完成之前,使用哪个 jQuery 函数来阻止代码运行?”,有所提出的三个答案都没有正确答案。 $(window).load() 应该是正确的答案,因为它指的是整个“页面”或“文档”,而不仅仅是 DOM。

  2. 要接受官方答案,“$(document).ready()”,原始问题应重写如下:“哪个 jQuery 函数用于防止代码在 DOM 完成加载之前从运行中运行?”

From the jQuery API documentation...

.ready()

Description: Specify a function to
execute when the DOM is fully
loaded. 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.

More about jQuery Ready

Defining DOM, from the W3C...

What is the Document Object Model?

The Document Object Model is a
platform- and language-neutral
interface that will allow programs and
scripts to dynamically access and
update the content, structure and
style of documents.

Defining .load() from the jQuery API documentation

.load()

Description: Bind an event handler to the "load" JavaScript event.

Example: Run a function when the page is fully loaded including
graphics.

$(window).load(function () {

 // run code 

});


After researching this and much contemplation, I've come to the conclusion that the wording of the question is incorrect given their three choices. Of course, based on this line of thinking, since there is no correct answer to the quiz question, my original answer was also wrong.

The original W3Schools jQuery Quiz Question:

Which jQuery function is used to
prevent code from running, before the
document is finished loading?

Let's now analyze the original three answers:

A. $(document).load()

This was my initial answer but after posting this question on StackOverflow, realized that $(document).load() is not valid code as far as I can tell. $(window).load() is what I originally had in mind. Using $(window).load(), you will prevent code from executing before the entire window has loaded including all it's elements, images, etc.

A cannot be the correct answer since the wording is "document.load" although should be "window.load".

B. $(document).ready()

This is the official answer and it's valid jQuery code. It triggers when the DOM is ready but before anything else has finished loading. I would argue that you cannot say the "document" has finished loading otherwise the word "document" and "DOM" would have the same meaning and be interchangeable.

B cannot be the correct answer because without the images and other assets, the page (document) has not finished loading.

C. $(body).onload()

C cannot be the correct answer simply because "onload()" is not part of the jQuery library.

Conclusion(s):

  1. As the question is worded, "Which jQuery function is used to prevent code from running, before the document is finished loading?", there is no correct answer from the three presented. $(window).load() should be the correct answer as it refers to the "page" or "document" as a whole and not just the DOM.

  2. To accept the official answer, "$(document).ready()", the original question should be re-written as follows: "Which jQuery function is used to prevent code from running, before the DOM is finished loading?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文