jquery - W3Schools jQuery 测验

标签 jquery jquery-selectors

这里的 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:

这是同一测验中的另一个问题。

Another questionable jQuery Quiz answer at W3Schools

最佳答案

您是对的,第一种语言可以(应该)从所有选择中删除。

根据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/

相关文章:

javascript - jQuery 函数从另一个 HTML 表创建 HTML 表

jquery - 如何在 DatePicker 中启用过去的日期?

javascript - 滚动在 JavaScript 中不起作用

javascript - Ruby on Rails - 更新 ajax 的 PUT 方法

jquery - 使用 ":not"和 ".not()"选择器之间的性能差异?

jquery - 从左到右一个接一个淡入

javascript - 为什么我的 div 按 .class 交换不起作用?

jquery - 使用 jQuery 对各个表中的选定单元格进行统一格式设置

jquery - 如何选择具有相同名称的所有输入并按名称对其进行索引

如果 *any* 属性匹配,JQuery 选择元素