jQuery :last-child 选择器
实例
选择作为其父元素的最后一个子元素的所有 <p> 元素:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("p:last-child").css("background-color", "yellow");});</script></head><body><p>body 中的第一个段落</p><div style="border:1px solid;"><p>div 中的第一个段落</p><p>div 中的最后一个段落</p></div><div style="border:1px solid;"><span>这是一个 span 元素.</span><p>另一个 div 中的第一个段落</p><p>另一个 div 中的最后一个段落</p></div><p>body 中的最后一个段落</p></body></html>
定义与用法
:last-child 选择器选择父元素的最后一个子元素。
提示: 使用 :first-child 选择器选择作为其父元素的第一个子元素的元素。
语法
$(":last-child")
更多实例
如何选择所有 <div> 元素中的最后一个 <p> 元素
演示 :last 与 :last-child 选择器的不同点
:last, :last-child 与 :last-of-type 选择器之间的不同点
演示 :last, :last-child 与 :last-of-type 选择器之间的不同点