jQuery :last-child 選擇器


jQuery 中的 :last-child 選擇器用於選擇所有作為其父元素最後一個子元素的元素。

語法

語法如下 −

$(":last-child")

示例

現在讓我們看一個示例來實現 jQuery :last-child 選擇器 −

<!DOCTYPE html>
<html>
<head>
<style>
   .one {
      color: white;
      background-color: green;
      font-size: 16px;
      border: 2px blue solid;
   }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("p:last-child").addClass("one");
   });
</script>
</head>
<body>
<h1>Examination Details</h1>
<p>Date: 25th October 2019</p>
<p>Timings: 10AM TO 1PM</p>
<p>All the best for exams!</p>
</body>
</html>

輸出

這將產生以下輸出 −

更新於: 2019 年 11 月 5 日

249 次瀏覽

職業生涯起航

完成課程以獲得認證

開始
廣告
© . All rights reserved.