jQuery :nth-last-of-type() 選擇器
jQuery 中的 :nth-last-of-type() 選擇器用於從最後一個子代開始對第 n 個子代的所有元素進行選擇。
語法
語法如下所示 −
:nth-last-of-type(n|even|odd|formula)
上文中,n 引數是與要匹配的每個子代的索引。even 引數選擇偶數子代元素,而 odd 選擇奇數子代元素。
示例
現在我們來看一個示例以實現 jQuery :nth-last-of-type() 選擇器 −
<!DOCTYPE html> <html> <head> <style> .one { color: white; background-color: orange; font-size: 16px; border: 2px blue dashed; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p:nth-last-of-type(2)").addClass("one"); }); </script> </head> <body> <h1>Match Details</h1> <div style="border:1px solid;"> <p>Date: 29th October 2019</p> <p>Timings: 3PM TO 8PM</p> <p>Ground: DY Patil</p> <p> Entry begins 2PM</p> </div><br> <div style="border:1px solid;"> <p>Date: 29th October 2019</p> <p>Timings: 8PM TO 11PM</p> <p>Ground: Eden Gardens</p> <p>Country: India</p> </div> </body> </html>
輸出
這將生成以下輸出 −
廣告