【发布时间】:2011-01-06 19:01:58
【问题描述】:
令我惊讶的是Sizzle(jQuery 使用的选择器引擎)带有内置的:nth-child() 选择器,但缺少:nth-of-type() 选择器。
为了说明:nth-child() 和:nth-of-type() 之间的区别并说明问题,请考虑the following HTML document:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>:nth-of-type() in Sizzle/jQuery?</title>
<style>
body p:nth-of-type(2n) { background: red; }
</style>
</head>
<body>
<p>The following CSS is applied to this document:</p>
<pre>body p:nth-of-type(2n) { background: red; }</pre>
<p>This is paragraph #1.</p>
<p>This is paragraph #2. (Should be matched.)</p>
<p>This is paragraph #3.</p>
<p>This is paragraph #4. (Should be matched.)</p>
<div>This is not a paragraph, but a <code>div</code>.</div>
<p>This is paragraph #5.</p>
<p>This is paragraph #6. (Should be matched.)</p>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script>
$(function() {
// The following should give every second paragraph (those that had red backgrounds already after the CSS was applied) an orange background.
// $('body p:nth-of-type(2n)').css('background', 'orange');
});
</script>
</body>
</html>
由于 Sizzle 使用浏览器原生的 querySelector() 和 querySelectorAll() 方法,如果它们存在(即在已经实现 Selectors API 的浏览器中),像 $('body p:nth-child'); 这样的东西当然可以工作。但它在旧版浏览器中无法使用,因为 Sizzle 没有此选择器的备用方法。
是否可以轻松地将 :nth-of-type() 选择器添加到 Sizzle,或者在 jQuery 中实现它(也许通过使用 the built-in :nth-child() selector)? custom selector with parameters 会很好。
【问题讨论】:
-
不确定,但
$('p:even')不会为您提供所需的内容吗?你已经有了选择器 (p),所以你只需要过滤它。 -
@Kobi:没那么容易。选择器
p:nth-child(2n)将匹配每个父元素中 中的每隔一个段落。如果有两个 DIV,都包含三个段落,则以下段落(按 DOM 顺序)将与p:nth-child(2n)匹配:#2、#5。看?这不仅仅是获取文档中的每个P,然后将其过滤到每个 mnth 元素的问题。是的,$('p:even')是$('p:nth-child(2n)')的别名,但不是$('p:nth-of-type(2n)')的别名。另外,我在这个例子中使用了2n,当然其他变体也应该是可能的。 -
知道了,并删除了我的答案。
-
Nick Craver,就像我在我的帖子中解释的那样,这是因为 Firefox 是具有选择器 API 的本机实现的浏览器之一。 Sizzle 不知道
:nth-of-type()选择器,但 Firefox 的querySelectorAll()知道。这就是它“有效”的原因——但这并不是因为 Sizzle。最好在 Sizzle 中使用它,因为它可以在 所有 浏览器中使用。 -
你不知道吗,它没有实现仅仅是因为John Resig didn't think it was worth implementing。
标签: jquery jquery-selectors css-selectors selectors-api