jquery获取元素的索引

In this post, we are going to discuss how to get an element with a specific index. jQuery API provides eq() method for this operation.

在本文中,我们将讨论如何获取具有特定索引的元素。 jQuery API为此操作提供了eq()方法。

jQuery eq()方法 (jQuery eq() method)

The jQuery eq() method is used to get an element with a specific index of the selected HTML element. You can give either positive or negative integer value as index. The index of the first element of the matched element is 0. If we use selector.eq(-1), it will return the last element and selector.eq(0) will return the first element in the matched set of elements.

jQuery eq()方法用于获取具有选定HTML元素的特定索引的元素。 您可以指定正整数或负整数作为索引。 匹配元素的第一个元素的索引为0。如果我们使用selector.eq(-1),它将返回最后一个元素,而selector.eq(0)将返回匹配的元素集中的第一个元素。

Here is the general syntax for using jQuery eq() method:

这是使用jQuery eq()方法的一般语法:

  • selector.eq(index)选择器.eq(索引)

index could be any positive or negative integer.

index可以是任何正整数或负整数。

jQuery eq()示例 (jQuery eq() example)

Following example demonstrates the jQuery eq() method usage.

以下示例演示了jQuery eq()方法的用法。

<!doctype html>
<html>
<head>
<title>jQuery Traversing eq</title>
<style>.highlight{background: yellow;}.highlight1{background: green;}div{display: block;border: 3px solid black;color: black;padding: 5px;margin: 25px;width:250px;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<h2>jQuery eq() demo</h2><div>My index is 0, also -6 from last</div>
<div>My index is 1, also -5 from last</div>
<div>My index is 2, also -4 from last</div>
<div>My index is 3, also -3 from last</div>
<div>My index is 4, also -2 from last</div>
<div>My index is 5, also -1 from last</div><script>
$( "div" ).eq( "1" ).addClass("highlight");
$( "div" ).eq( "-6" ).addClass("highlight1");</script>
</body>
</html>

In this example, we can see the use of jQuery eq() method. The $("div").eq(1) will return the second div element and changes the color to yellow. Similarly the elements are searched when index is negative, so -6 will return the first div element in this html page. Below image shows the output produced by above HTML page.

在此示例中,我们可以看到jQuery eq()方法的使用。 $("div").eq(1)将返回第二个div元素,并将颜色更改为黄色。 同样,当index为负时将搜索元素,因此-6将返回此html页面中的第一个div元素。 下图显示了以上HTML页面产生的输出。

That’s all for now. We will discuss some more traversing techniques in the coming posts.

目前为止就这样了。 我们将在以后的文章中讨论更多的遍历技术。

翻译自: https://www.journaldev.com/5339/how-to-get-an-element-with-specific-index-jquery

jquery获取元素的索引

jquery获取元素的索引_如何在jQuery中获取具有特定索引的元素相关推荐

  1. java 获取文件扩展名_如何在Java中获取文件扩展名

    java 获取文件扩展名 Sometimes while working with files, we need to process them differently based on their ...

  2. c#获取ssl证书有效性_如何在c#中获取远程服务器的SSL证书信息

    我尝试使用以下它工作正常: string strDNSEntry是您需要SSL的DNS public X509Certificate2 DownloadSslCertificate(string st ...

  3. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

  4. php mysql 到表最后_如何在PHP中获取MySQL表的最后插入ID?

    如何在PHP中获取MySQL表的最后插入ID? 我有一张表,经常插入新数据. 我需要获取表格的最后一个ID. 我怎样才能做到这一点? 它类似于SELECT MAX(id) FROM table吗? g ...

  5. java文件中获取创建日期_如何在Java中获取文件的上次修改日期

    java文件中获取创建日期 Sometimes we need to get the file last modified date in Java, usually for listeners li ...

  6. python获取当前时间戳_如何在Python中获取当前时间戳?

    在Python中可以使用来自模块time.datetime或calendar的函数来获取当前时间戳,代码语句如[import time;ts = time.time() print(ts)]. 在Py ...

  7. html 获取文本框值,html - 如何在JavaScript中获取文本框值

    html - 如何在JavaScript中获取文本框值 我正在尝试使用JavaScript从HTML文本框中获取值,但值不是在空格之后 例如: 我只得到:上面的"软件". 我正在使 ...

  8. python中获取文件大小_如何在Python中获取文件大小

    python中获取文件大小 We can get file size in Python using the os module. 我们可以使用os模块在Python中获取文件大小. Python中的 ...

  9. java 字符串 数组 索引_如何在Java中找到数组中元素的索引?

    我希望在Java中找到给定元素的索引,知道它的内容. 我尝试了以下示例,该示例不起作用: class masi { public static void main( String[] args ) { ...

最新文章

  1. 纠结pytorch, tensorflow, keras 一个月之后,我自己创造了一个神经网络框架
  2. linux 常用的系统信息查看命令
  3. Linux下如何查看哪些端口处于监听状态
  4. python基础知识整理-整理了27个新手必学的Python基础知识点
  5. 学校机房项目交换机的配置
  6. python的for语句中i未被定义_python – PyLint:使用可能未定义的循环变量警告
  7. 【ArcGIS风暴】ArcGIS创建栅格数据集色彩映射表案例--以GlobeLand30土地覆盖数据为例
  8. 故障诊断 | 存储Cache丢失导致数据库无法open的案例分享
  9. c++ 多线程 垃圾回收器_JavaSE基础代码(3)--JavaSE程序入口,JDK,JRE,JVM垃圾回收器的关系与作用...
  10. 2015/8/26 Python基础(1):基本规则及赋值
  11. 电子换向电动机行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  12. 复杂查询练习_MySQL基础知识—习题练习
  13. 固定td 样式 随 内容 溢出
  14. 微积分知识总览(0)
  15. IntentService简介
  16. Java经典编程题50道之三十二
  17. 视频教程-PHP之socket入门实战websocket聊天室-PHP
  18. python刷课系统教师_让教师只想刷课的继续教育不能继续了
  19. JavaScript:一个非常有趣的猜数小游戏
  20. java 继承、super、this、抽象类

热门文章

  1. C++头文件重复定义问题的处理(不会看看,会了防身!)
  2. [转载] 当心掉进Python多重继承里的坑
  3. [转载] 如何用一个Python示例入门TensorFlow?
  4. python 基础课程第三天
  5. 正式版的Linux Kernel 5.1来了,非LTS
  6. 汇编:输出寄存器AX中的内容
  7. Ansible(自动化运维工具--playbook)
  8. 201521123058 软工阅读第二次作业
  9. EasyUI +MVC +EF实现增删改查
  10. C/C++ 位域总结