加载中...

:eq(index)


概述    eq selector

返回值:Array<Element>

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

  • V : 1.0jQuery( ":eq(index)" )

    index: 要匹配元素的索引值(从0开始计数)

  • V : 1.8jQuery( ":eq(-index)" )

    indexFromEnd: 要匹配元素的索引值(从0开始计数), 从最后一个元素开始倒计数。(手册网注:-1匹配倒数第一个元素)

这种索引值相关的选择器(:eq(), :lt(), :gt(), :even, :odd)过滤他们前面的匹配表达式的集合元素。进一步筛选的依据就是这个元素在原先匹配集合中的顺序。例如,如果第一个选择器使用类选择器( .myclass )进行匹配,四个元素返回,这些元素是给定索引是03

请注意,由于JavaScript数组使用基于0的索引 ,这些选择器也是如此。这就是为什么$('.myclass:eq(1)')选择器选择文档中第二个MyClass类的元素,而不是第一个。与此相反,:nth-child(n)基于1的索引的,以符合CSS规范。

在jQuery 1.8之前,:eq(index)接受负值所引值(虽然.eq(index)方法接受)。

其他注意事项:

  • 因为 :eq() 是一个 jQuery 延伸出来的选择器,并不是的CSS规范的一部分, 使用:eq()查询不能充分利用原生DOM提供的querySelectorAll() 方法来提高性能。为了在现代浏览器上获得更佳的性能,请使用$("your-pure-css-selector").eq(index)代替。

示例

实例

查找第二行

HTML 代码:
  1. <table>
  2. <tr><td>Header 1</td></tr>
  3. <tr><td>Value 1</td></tr>
  4. <tr><td>Value 2</td></tr>
  5. </table>
jQuery 代码:
  1. $("tr:eq(1)")
结果:
  1. [ <tr><td>Value 1</td></tr> ]

实例

查找第三个 td。

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

运行一下

实例

在列表项目中应用三种不同的样式,以此来展示 :eq() 只会选择一个元素,而 :nth-child() 或 :eq() 会像 .each() 一样,有类似循环的构造,从而选择多个元素。

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

运行一下

实例

Add a class to List 2, item 2 by targeting the second to last <li>

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

运行一下


还没有评论.