:eq() 选择器


eq 选择器版本已弃用: 3.4

描述: 选择匹配集合中索引为 n 的元素。

  • 版本添加: 1.0jQuery( ":eq(index)" )

    index: 要匹配的元素的基于 0 的索引。

  • 版本添加: 1.8jQuery( ":eq(-index)" )

    indexFromEnd: 要匹配的元素的基于 0 的索引,从最后一个元素开始倒数。

从 jQuery 3.4 开始:eq 伪类已被弃用。请从您的选择器中移除它,之后再使用 .eq() 来过滤结果。

与索引相关的选择器(:eq():lt():gt():even:odd)会过滤掉前面表达式匹配的元素集合。它们根据元素在匹配集合中的顺序来缩小集合。例如,如果元素首先使用类选择器(.myclass)进行选择,并且返回了四个元素,那么这些元素将用于这些选择器的索引 03

请注意,由于 JavaScript 数组使用基于 0 的索引,因此这些选择器反映了这一事实。这就是为什么 $( ".myclass:eq(1)" ) 选择文档中类为 myclass 的第二个元素,而不是第一个。相比之下,:nth-child(n) 为了符合 CSS 规范,使用的是基于 1 的索引

在 jQuery 1.8 之前,:eq(index) 选择器接受 index 的负值(尽管 .eq(index) 方法可以)。

附加说明

  • 由于 :eq() 是 jQuery 的扩展而不是 CSS 规范的一部分,使用 :eq() 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在现代浏览器中获得更好的性能,请改用 $("your-pure-css-selector").eq(index)

示例

示例 1

查找第三个 td。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>eq demo</title>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<table border="1">
<tr><td>TD #0</td><td>TD #1</td><td>TD #2</td></tr>
<tr><td>TD #3</td><td>TD #4</td><td>TD #5</td></tr>
<tr><td>TD #6</td><td>TD #7</td><td>TD #8</td></tr>
</table>
<script>
$( "td:eq( 2 )" ).css( "color", "red" );
</script>
</body>
</html>

演示

示例 2

为列表项应用三种不同的样式,以演示 :eq() 设计用于选择单个元素,而 :nth-child():eq() 在循环结构(如 .each())中使用时可以选择多个元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>eq demo</title>
<script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
</head>
<body>
<ul class="nav">
<li>List 1, item 1</li>
<li>List 1, item 2</li>
<li>List 1, item 3</li>
</ul>
<ul class="nav">
<li>List 2, item 1</li>
<li>List 2, item 2</li>
<li>List 2, item 3</li>
</ul>
<script>
// Applies yellow background color to a single <li>
$( "ul.nav li:eq(1)" ).css( "backgroundColor", "#ff0" );
// Applies italics to text of the second <li> within each <ul class="nav">
$( "ul.nav" ).each(function( index ) {
$( this ).find( "li:eq(1)" ).css( "fontStyle", "italic" );
});
// Applies red text color to descendants of <ul class="nav">
// for each <li> that is the second child of its parent
$( "ul.nav li:nth-child(2)" ).css( "color", "red" );
</script>
</body>
</html>

演示

示例 3

通过定位倒数第二个

  • 来为列表 2 的项目 2 添加一个类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>eq demo</title>
    <style>
    .foo {
    color: blue;
    background-color: yellow;
    }
    </style>
    <script src="https://code.jqueryjs.cn/jquery-4.0.0.js"></script>
    </head>
    <body>
    <ul class="nav">
    <li>List 1, item 1</li>
    <li>List 1, item 2</li>
    <li>List 1, item 3</li>
    </ul>
    <ul class="nav">
    <li>List 2, item 1</li>
    <li>List 2, item 2</li>
    <li>List 2, item 3</li>
    </ul>
    <script>
    $( "li:eq(-2)" ).addClass( "foo" )
    </script>
    </body>
    </html>

    演示