
- jQuery 教程
- jQuery - 首頁
- jQuery - 路線圖
- jQuery - 概述
- jQuery - 基礎
- jQuery - 語法
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 屬性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 新增元素
- jQuery - 刪除元素
- jQuery - 替換元素
- jQuery CSS 操作
- jQuery - CSS 類
- jQuery - 尺寸
- jQuery - CSS 屬性
- jQuery 效果
- jQuery - 效果
- jQuery - 動畫
- jQuery - 鏈式操作
- jQuery - 回撥函式
- jQuery 遍歷
- jQuery - 遍歷
- jQuery - 遍歷祖先節點
- jQuery - 遍歷後代節點
- jQuery UI
- jQuery - 互動
- jQuery - 小部件
- jQuery - 主題
- jQuery 參考
- jQuery - 選擇器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍歷
- jQuery - 雜項
- jQuery - 屬性
- jQuery - 工具函式
- jQuery 外掛
- jQuery - 外掛
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用資源
- jQuery - 問答
- jQuery - 快速指南
- jQuery - 有用資源
- jQuery - 討論
jQuery :last-child 選擇器
jQuery 中的:last-child 選擇器用於選擇其父元素內的最後一個子元素。此選擇器通常用於對父元素的最後一個子元素應用樣式或執行操作。此選擇器適用於所有 HTML 元素。
如果我們想選擇其父元素的第一個子元素,我們需要使用:first-child 選擇器。
語法
以下是 jQuery 中 :last-child 選擇器的語法:
$(":last-child")
引數
:last-child 選擇指定父元素的最後一個子元素。
示例 1
在以下示例中,我們使用“:last-child”選擇器來選擇每個無序列表中的最後一個列表項:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("ul li:last-child").css("background-color", "yellow"); }); </script> </head> <body> <ul> <li>First item</li> <li>Second item</li> <li>Third item</li> </ul> <ul> <li>First item in second list</li> <li>Second item in second list</li> </ul> </body> </html>
執行上述程式後,每個無序列表中的最後一個列表項將以黃色背景突出顯示。
示例 2
在此示例中,我們選擇每個表格中的最後一行表格行:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("table tr:last-child").css("background-color", "yellow"); }); </script> </head> <body> <table border="1"> <tr> <td>First row, first cell</td> <td>First row, second cell</td> </tr> <tr> <td>Second row, first cell</td> <td>Second row, second cell</td> </tr> </table> <br><br> <table border="1"> <tr> <td>First row, first cell in second table</td> <td>First row, second cell in second table</td> </tr> <tr> <td>Second row, first cell in second table</td> <td>Second row, second cell in second table</td> </tr> </table> </body> </html>
當我們執行上述程式時,每個表格中的最後一行表格行將以黃色背景突出顯示。
示例 3
在這裡,我們選擇所有 <div> 元素的最後一個 <p> 元素:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script> $(document).ready(function(){ // Apply an italic font style to the first <p> child of each <div> $("div p:first-child").css("background-color", "yellow"); }); </script> </head> <body> <div style="border: 1px solid black;"> <p>First paragraph in first div</p> <p>Second paragraph in first div</p> </div> <br> <div style="border: 1px solid black;"> <p>First paragraph in second div</p> <p>Second paragraph in second div</p> </div> </body> </html>
執行上述程式後,所有 <div> 元素的最後一個 <p> 元素將以黃色背景突出顯示。
jquery_ref_selectors.htm
廣告