jQuery中 :first、:first-child 和 :first-of-type 之间的不同

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var btn = $(this).text();
$("p").css("background-color","white");
$("p" + btn).css("background-color","yellow");
});
});
</script>
</head>
<body>
<button>:first</button>
<button>:first-child</button>
<button>:first-of-type</button><br><br>
<p>body 中第一个段落。</p>

<div style="border:1px solid;">
<p>div 中第一个段落,是 div 的第一个子元素。</p>
<p>div 中的最后一个段落,是 div 的最后一个子元素。</p>
</div><br>

<div style="border:1px solid;">
<span>这是一个 span 元素,是 div 中的第一个子元素。</span>
<p>另一个 div 中第一个段落,是 div 中的第二个子元素。</p>
<p>另一个 div 中的最后一个段落, 是 div 中的第三个子元素。</p>
<span>这是一个 span 元素,是 div 中的最后一个子元素。</span>
</div><br>

<div style="border:1px solid">
<p>另一个 div 中第一个段落,是 div 中的第一个子元素。</p>
<p>另一个 div 中的最后一个段落, 是 div 中的最后一个子元素。</p>
</div>

<p>body 中最后一个段落。</p>

</body>
</html>

原文地址:https://www.cnblogs.com/fireporsche/p/6395752.html