令我感到惊讶的是,Sizzle(jQuery使用的选择器引擎)带有内置:nth-child()
选择器,但缺少:nth-of-type()
选择器.
为了说明和之间的区别:nth-child()
并:nth-of-type()
说明问题,请考虑以下HTML文档:
<!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 …
Run Code Online (Sandbox Code Playgroud)