jQuery::nth-of-type() 選擇器
jQuery 中的 :nth-of-type() 選擇器用於選擇是其父級中的特定型別第 n 個子代的所有元素。
語法
語法如下 −
:nth-of-type(n|even|odd|formula)
其中,n 引數是要匹配每個子代的索引。even 引數選擇偶數子代元素,而 odd 則選擇奇數子代元素。
示例
讓我們現在看一個示例以實現 jQuery :nth-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-of-type(3)").addClass("one");
});
</script>
</head>
<body>
<h1>Match Details</h1>
<p>One</p>
<p>Two</p>
<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>
<p>Three</p>
</body>
</html>輸出
這會生成以下輸出 −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP