如何使用 jQuery 選擇器 eq:()?


如果您想要選擇具有特定索引的元素,請使用 jQuery 選擇器 eq()。在此,設定索引號以選擇元素。

示例

您可以嘗試執行以下程式碼來學習如何使用 jQuery 選擇器 eq()

即時演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
   $("p:eq(1)").css("background-color","red");
});
</script>
</head>
<body>

<h1>Heading 1</h1>
<p class="myclass">This is demo text.</p>
<p>This is another text.</p>
<p>This is demo content</p>

</body>
</html>

更新於:2019 年 12 月 18 日

206 次瀏覽

啟動您的 職業生涯

完成課程以獲取認證

開始
廣告
© . All rights reserved.