:first-child 不返回任何元素
:first-child not returning any elements
我一定是遗漏了 :first-child
伪选择器的一些细微差别。我的代码中有以下内容(其中 je
是一个 jQuery 对象,对应于 DOM 中的一个节点:
const errors = je.find('div.error');
console.log(errors.length + ' errors found');
const firstError = je.find('div.error:first-child');
console.log(firstError.length + ' first errors found ');
以上日志:
1 errors found
0 first errors found
如果 找到了 某些元素,难道我不应该期望 :first-child
伪选择器 return 第一个吗?
jFiddle SSCCE here
If some elements were found, shouldn't I expect the :first-child
pseudo-selector to return the first of them?
你为什么要这样做?如果您有一组元素可能与父元素相关或不相关,那么这些元素中的 :first-child
有何意义?顾名思义,即使它们都具有相同的父元素,:first-child
仍然只会匹配确实是其父元素的第一个子元素的元素。如果集合中的 all 元素是其各自父元素的第一个子元素,则 :first-child
将匹配其中的 all。
一组匹配中的第一个元素由 :first
表示,而不是 :first-child
。
我一定是遗漏了 :first-child
伪选择器的一些细微差别。我的代码中有以下内容(其中 je
是一个 jQuery 对象,对应于 DOM 中的一个节点:
const errors = je.find('div.error');
console.log(errors.length + ' errors found');
const firstError = je.find('div.error:first-child');
console.log(firstError.length + ' first errors found ');
以上日志:
1 errors found
0 first errors found
如果 找到了 某些元素,难道我不应该期望 :first-child
伪选择器 return 第一个吗?
jFiddle SSCCE here
If some elements were found, shouldn't I expect the
:first-child
pseudo-selector to return the first of them?
你为什么要这样做?如果您有一组元素可能与父元素相关或不相关,那么这些元素中的 :first-child
有何意义?顾名思义,即使它们都具有相同的父元素,:first-child
仍然只会匹配确实是其父元素的第一个子元素的元素。如果集合中的 all 元素是其各自父元素的第一个子元素,则 :first-child
将匹配其中的 all。
一组匹配中的第一个元素由 :first
表示,而不是 :first-child
。