子元素偽類選擇器,顧名思義就是用來挑選某個元素底下的子元素。這是 jQuery 裡極常用的操作,掌握好之後,你就不需要為了操作子元素而在每個標籤上塞 id 或 class。
它共分兩大類。
| 選擇器 | 說明 |
|---|---|
| :first-child | 選擇父元素下的第 1 個子元素 |
| :last-child | 選擇父元素下的最後 1 個子元素 |
| :nth-child(n) | 選擇父元素下的第 n 個元素或奇偶元素,n 為「整數 | odd | even」 |
| :only-child | 選擇父元素中唯一的子元素(父元素只有一個子元素) |
舉例 1:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
*{padding:0;margin:0;}
ul { display:inline-block; width:200px; list-style-type:none; }
li{height:20px;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("ul li:first").css("background-color", "red");
$("ul li:nth-child(2)").css("background-color", "orange");
$("ul li:nth-child(3)").css("background-color", "yellow");
$("ul li:nth-child(4)").css("background-color", "green");
$("ul li:last").css("background-color", "blue");
})
</script>
</head>
<body>
<ul>
<li></li><li></li><li></li><li></li><li></li>
</ul>
</body>
</html>
分析:
$("ul li:first") 選出 ul 下的第 1 個 li,效果等同 $("ul li:nth-child(1)");$("ul li:last") 選最後一個,在本例中等於 $("ul li:nth-child(5)")。
還沒接觸過子元素偽類選擇器的人,要操作子元素時通常得幫每個子元素加上 id 或 class,結果 id、class 滿街跑,HTML 結構被汙染,維護成本也高。有了這組選擇器,選取動作變得非常輕巧。
學過 CSS3 的人可能會問:這件事用 CSS 做不是更簡單?確實如此。入門階段的範例重在把觀念講透,而不在示範最佳實踐;真正的綜合應用會放在進階內容裡。
舉例 2:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
*{padding:0;margin:0;}
ul { display:inline-block; width:200px; list-style-type:none; }
li{height:20px;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(function () {
//設置奇數行的背景顏色
$("ul li:nth-child(odd)").css("background-color", "red");
//設置偶數行的背景顏色
$("ul li:nth-child(even)").css("background-color", "orange");
})
</script>
</head>
<body>
<ul>
<li></li><li></li><li></li><li></li><li></li>
</ul>
</body>
</html>
分析:
odd 與 even 讓「斑馬線」效果變得一行程式碼就能搞定。
| 選擇器 | 說明 |
|---|---|
| :first-of-type | 選擇同類型元素中的第 1 個同級兄弟 |
| :last-of-type | 選擇同類型元素中的最後 1 個同級兄弟 |
| :nth-of-type | 選擇同類型元素中的第 n 個同級兄弟,n 為「整數 | odd | even」 |
| :only-of-type | 選擇父元素中該類型的唯一子元素(父元素可有多個其他類型子元素) |
兩組選擇器長得很像,本質卻不同。請記牢這句話:第一組不區分元素類型,第二組區分元素類型。此外,第二組命名上都帶 type,這也是一個辨識與記憶的線索。
假設結構如下:
<div> <h1></h1> <p></p> <span></span> <span></span> </div>
h1:first-child:選得到。因為 h1 是 div 的第 1 個子元素。
p:first-child:選不到。p 是 div 的第 2 個子元素,不是第 1 個。
span:first-child:選不到。span 是 div 的第 3 個子元素。
h1:first-of-type:選得到。h1 是 div 裡第一個(也是唯一一個)h1。
p:first-of-type:選得到。p 是 div 裡第一個 p。
span:first-of-type:選得到,而且選的是兩個 span 中的第 1 個。這正是與 :first-child 最大的不同——它只看「同類兄弟中的排位」,不看自己在所有子元素中的排位。
總結:
① :first-child 選的是父元素下「名副其實的第 1 個」子元素(不區分類型);:first-of-type 選的是父元素下「某類型中的第 1 個」(區分類型)。
② :last-child 與 :last-of-type、:nth-child(n) 與 :nth-of-type(n) 的道理完全一樣,你可以自己套進上面的結構推演一次。
在 :only-child 中,父元素必須只有一個子元素;而 :only-of-type 允許父元素有多個子元素,只要該類型只有一個即可。差別就在多了 type 這層限制。
舉例:
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
*{padding:0;margin:0;}
</style>
<script type="text/javascript" src="jquery-1.12.0.min.js"></script>
<script type="text/javascript">
$(function () {
$("div p:only-child").css("background-color", "red");
})
</script>
</head>
<body>
<div>
<p>這是一個段落</p>
</div>
<div>
<p>這是一個段落</p>
<span>這是一個span</span>
</div>
</body>
</html>
分析:
第一個 div 內部只有一個子元素,所以它的 p 被選中;第二個 div 有 p 和 span 兩個子元素,因此沒被選中。
接著把 $("div p:only-child") 改成 $("div p:only-of-type"),你會發現兩個 p 都被選中了——因為每個 div 裡的 p 都只有一個。試著自己說明原因,這題想通了,兩組選擇器的差別就真的懂了。
子元素偽類選擇器在實戰中出現頻率非常高。有些初學者覺得太瑣碎,跳著看、掃兩眼就以為懂了,這其實是本末倒置。這裡提到的每一個重點,都是後面寫專案時會反覆用到的工具。