它是一个jQuery选择器用于选择每个元素最后一个孩子其父母.
语法如下:
$(":last-child")
返回值:它选择并返回其父级的最后一个子级元素。
示例1:
<!DOCTYPE html>
< html >
< h1 >
< center >
Geeks
</ center >
</ h1 >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("p:last-child").css(
"background-color", "green");
});
</ script >
</ head >
< body >
< div >
< p >Geeks for Geeks</ p >
< p >jQuery</ p >
< p >Last Child Selector</ p >
</ div >
</ body >
</ html >
输出如下:

示例2:
<!DOCTYPE html>
< html >
< h1 >
< center >
Geeks
</ center >
</ h1 >
< head >
< script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
</ script >
< script >
$(document).ready(function() {
$("p:last-child").css(
"background-color", "green");
});
</ script >
</ head >
< body >
< div style = "border:1px solid;" >
< p >Geeks for Geeks</ p >
< p >jQuery</ p >
</ div >
< br >
< div style = "border:1px solid;" >
< p >Geeks for Geeks</ p >
< p >jQuery</ p >
< p >Last Child Selector</ p >
</ div >
< p >jQuery:Last Child Selector</ p >
</ body >
</ html >
输出如下:
