jQuery eq() 方法


jQuery 的 eq() 方法用於選擇具有特定索引號的元素。索引號從 0 開始。

語法

語法如下 −

$(":eq(index)")

上方,引數 index 為元素的索引。

示例

現在我們來看一個 jQuery eq() 方法的實現示例 −

 實際演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("p:eq(2)").css("color", "orange");
      });
   });
</script>
</head>
<body>
<h2>Student Info</h2>
<p>This is a demo text.</p>
<h2>Exam Info</h2>
<p>This is a demo text.</p>
<h2>Teacher's Info</h2>
<p>This is a demo text.</p>
<button>Click me</button>
</body>
</html>

輸出

這會產生以下輸出 −

單擊“單擊我”以更改特定標題的標題顏色 −

更新日期:31-Dec-2019

207 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.