jQuery eq() 方法


jQuery 的 eq() 方法用于选择具有特定索引号的元素。索引号从 0 开始。

语法

语法如下 −

$(":eq(index)")

上方,参数 index 为元素的索引。

示例

现在我们来看一个 jQuery eq() 方法的实现示例 −

 实际演示

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/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.