:nth-child(even|odd)
匹配集合中子集里奇数元素或是偶数元素的元素集合。返回匹配元素集合
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("ul li:nth-child(even)").append("<span> - even!</span>");
$("ul li:nth-child(odd)").append("<span> - odd!</span>");
});
</script>
<style>
div { float:left; }
span { color:blue; }
</style>
</head>
<body>
<div><ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul></div>
<div><ul>
<li>Sam</li>
</ul></div>
<div><ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
<li>David</li>
</ul></div>
</body>
</html>
$("ul li:nth-child(even)").append("<span> - even!</span>");
匹配ul里包含li的li元素集合,且仅仅是li子集中的偶数li元素集合。以下为匹配元素
<li>Karl</li>
<li>Tane</li>
<li>David</li>