这里的 W3Schools 网站上发布了一个 jQuery 测验...
http://www.w3schools.com/quiztest/quiztest.asp?qtest=jQuery
问题#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"
我选择了答案 B,结果是正确的。
我的问题与答案 B 的措辞有关。
难道不应该从答案中删除“第一”这个词吗?
B. All elements with class="head" inside the div element with id="intro"
ID 被定义为“元素的唯一标识符”,因此不太理解为什么他们会引用“第一个带有 id 的 div 元素” =简介”
我不认为这是故意刁难,因为此测验中的所有其他问题都非常简单。
谢谢您的想法。
<小时/>编辑:
我向 W3Schools 报告了此错误,并将他们定向到此线程。
<小时/>编辑#2:
这是同一测验中的另一个问题。
最佳答案
您是对的,第一种语言可以(应该)从所有选择中删除。
根据HTML 4.01 Spec :
This attribute assigns a name to an element. This name must be unique in a document.
此外,根据 jQuery documentation for the id selector :
Selects a single element with the given id attribute
在底层,选择器使用 document.getElementById("...")
。有趣的是,the specification对于这个函数来说:
Behavior is not defined if more than one element has this ID.
因此,假设您确实有两个具有相同 id
的元素,则该函数的结果是不可预测的且特定于浏览器。
旁注:W3Schools 并不被认为是学习 JavaScript/jQuery 的最佳场所之一。一个备受推崇的 JavaScript 替代方案是 MDC's JavaScript Guide 。对于 jQuery,请查看 tutorials页。
关于jquery - W3Schools jQuery 测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5710350/