:first-child選擇器選擇所有屬于其父級第一個(gè)子級的元素。
使用:last-child選擇器來選擇屬于其父級的最后一個(gè)子級的元素。
$(":first-child")
選擇屬于其父級的第一個(gè)子級的每個(gè)<p>元素:
$(document).ready(function(){ $("p:first-child").css("background", "coral"); });測試看看?/?
選擇所有<div>元素中的第一個(gè)<p>元素:
$(document).ready(function(){ $("div p:first-child").css("background", "coral"); });測試看看?/?