W3Schools jQuery 测验
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5710350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
W3Schools jQuery Quiz
提问by Sparky
There is a jQuery quiz posted on the W3Schools site here...
W3Schools 网站上发布了一个 jQuery 测验...
http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery
http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery
Question #19 is as follows,
问题#19如下,
Look at the following jQuery selector: $("div#intro .head").
What does it select?
A. The first element with id="head" inside any div element with class="intro"
B. All elements with class="head" inside the first div element with id="intro"
C. All div elements with id="intro" or class="head"
查看以下 jQuery 选择器:$("div#intro .head")。
它选择什么?
A. class="intro" 的任何 div 元素中第一个 id="head" 的元素
B. 第一个 id="intro" 的 div 元素内所有 class="head" 的元素
C. id="intro" 或 class="head" 的所有 div 元素
I got it correct by picking answer B.
我通过选择答案 B 得到了正确答案。
My question has to do with the wording of answer B.
我的问题与答案 B 的措辞有关。
Shouldn't the word, "first", be removed from the answer?
不应该从答案中删除“第一”这个词吗?
B. All elements with class="head" inside the div element with id="intro"
B. id="intro" 的 div 元素中所有 class="head" 的元素
ID is defined as "a uniqueidentifier to an element", so not really understanding why they would refer to the "the firstdiv element with id=intro"
ID 被定义为“元素的唯一标识符”,所以不太明白为什么他们会引用“ id=intro的第一个div 元素”
I don't believe that it's intentionally trying to be tricky as all the other questions in this quiz are very straightforward.
我不认为它是故意变得棘手,因为这个测验中的所有其他问题都非常简单。
Thank-you for your thoughts.
谢谢你的想法。
EDIT:
编辑:
I reported this error to W3Schools and directed them to this thread.
我向 W3Schools 报告了这个错误并将他们定向到这个线程。
EDIT #2:
编辑#2:
This is another question from the same quiz.
这是来自同一个测验的另一个问题。
回答by Andrew Whitaker
You are correct, the firstlanguage could (should) be removed from all choices.
您是对的,可以(应该)从所有选项中删除第一种语言。
According to the HTML 4.01 Spec:
根据HTML 4.01 规范:
This attribute assigns a name to an element. This name must be unique in a document.
此属性为元素分配名称。此名称在文档中必须是唯一的。
Additionally, according to the jQuery documentation for the id selector:
Selects a single element with the given id attribute
选择具有给定 id 属性的单个元素
Under the hood, the selector uses document.getElementById("...")
. Interestingly, the specificationfor this function states:
在幕后,选择器使用document.getElementById("...")
. 有趣的是,此函数的规范指出:
Behavior is not defined if more than one element has this ID.
如果多个元素具有此 ID,则行为未定义。
So, assuming you do have two elements with the same id
, results of the function are unpredictable and browser-specific.
因此,假设您确实有两个具有相同 的元素,则id
该函数的结果是不可预测的且特定于浏览器的。
Sidenote: W3Schools is not regarded as one of the best places to learn JavaScript / jQuery. A well-respected alternative for JavaScript is MDC's JavaScript Guide. For jQuery, check out the tutorialspage.
旁注:W3Schools 不被视为学习 JavaScript / jQuery 的最佳场所之一。一个备受推崇的 JavaScript 替代品是MDC 的 JavaScript 指南。对于 jQuery,请查看教程页面。
回答by Gil Dawson
Indeed the behavior of document.getElementById("...") is undefined if more than one element has this ID.
实际上,如果多个元素具有此 ID,则 document.getElementById("...") 的行为未定义。
However, as the w3schools site consistently tries to point out, the behavior of jQuery in this situation is well-defined. If more than one element has this ID, then the firstone will be selected.
然而,正如 w3schools 站点一直试图指出的那样,jQuery 在这种情况下的行为是明确定义的。如果多个元素具有此 ID,则将选择第一个。
No one is condoning more than one element's having a particular ID; this is still against the rules. However, unlike getElementById, jQuery has a specific reaction if this rule is broken.
没有人会容忍多个元素具有特定 ID;这仍然违反规则。但是,与 getElementById 不同的是,如果违反此规则,jQuery 会有特定的反应。