失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > jquery获取元素的索引_如何在jQuery中获取具有特定索引的元素

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

时间:2023-12-31 21:42:42

相关推荐

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

jquery获取元素的索引

In this post, we are going to discuss how to get an element with a specific index. jQuery API provideseq()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(索引)

indexcould 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="/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.

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

翻译自: /5339/how-to-get-an-element-with-specific-index-jquery

jquery获取元素的索引

如果觉得《jquery获取元素的索引_如何在jQuery中获取具有特定索引的元素》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。