加载中...

:nth-last-of-type()1.9+


概述    nth-last-of-type selector

返回值:jQuery

描述:选择所有他们父级兄弟元素下具有相同的元素名(手册网注:标签名,比如<li>)的倒数第n个子元素,计数从最后一个元素开始到第一个。

  • V : 1.9jQuery( ":nth-last-of-type(index/even/odd/equation)" )

    index: 每个相匹配子元素的索引值,从最后一个开始(1),也可以是字符串 evenodd,或一个方程式( 例如 :nth-last-of-type(even), :nth-last-of-type(4n))

因为jQuery的实现:nth-是严格来自CSS规范,n值是“1-indexed”,也就是说,从1开始计数。对于所有其他选择器表达式比如:eq():even ,jQuery遵循JavaScript的“0索引”的计数。因此,给定一个单一<ul>包含3个<li>$('li:nth-last-of-type(1)')选择第3个,也就是最后一个<li>

这个不寻常的用法,可进一步讨论中找到W3C CSS specification.

示例

在每个匹配的ul中查找倒数第二个li

  1. <ul>
  2. <li>1</li>
  3. <li>2</li>
  4. <li>3</li>
  5. <li>4</li>
  6. </ul>
  7.  
  8. $("ul li:nth-last-of-type(2)");

实例

在每个匹配的ul中查找倒数第二个li,并将它标记出来。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>nth-last-of-type demo</title>
  6. <style>
  7. div {
  8. float: left;
  9. }
  10. span {
  11. color: blue;
  12. }
  13. </style>
  14. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  15. </head>
  16. <body>
  17. <div>
  18. <ul>
  19. <li>John</li>
  20. <li>Karl</li>
  21. <li>Adam</li>
  22. </ul>
  23. </div>
  24. <div>
  25. <ul>
  26. <li>Dan</li>
  27. </ul>
  28. </div>
  29. <div>
  30. <ul>
  31. <li>Dave</li>
  32. <li>Rick</li>
  33. <li>Timmy</li>
  34. <li>Gibson</li>
  35. </ul>
  36. </div>
  37. <script>
  38. $( "ul li:nth-last-of-type(2)" ).append( "<span> - 2nd to last!</span>" );
  39. </script>
  40. </body>
  41. </html>

运行一下

实例

This is a playground to see how the selector works with different strings.

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>nth-last-of-type demo</title>
  6. <style>
  7. button {
  8. display: block;
  9. font-size: 12px;
  10. width: 100px;
  11. }
  12. div {
  13. float: left;
  14. margin: 10px;
  15. font-size: 10px;
  16. border: 1px solid black;
  17. }
  18. span {
  19. color: blue;
  20. font-size: 18px;
  21. }
  22. #inner {
  23. color: red;
  24. }
  25. td {
  26. width: 50px;
  27. text-align: center;
  28. }
  29. </style>
  30. <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  31. </head>
  32. <body>
  33. <div>
  34. <button>:nth-last-of-type(even)</button>
  35. <button>:nth-last-of-type(odd)</button>
  36. <button>:nth-last-of-type(3n)</button>
  37. <button>:nth-last-of-type(2)</button>
  38. </div>
  39. <div>
  40. <button>:nth-last-of-type(3n+1)</button>
  41. <button>:nth-last-of-type(3n+2)</button>
  42. </div>
  43. <div>
  44. <table>
  45. <tr><td>John</td></tr>
  46. <tr><td>Karl</td></tr>
  47. <tr><td>Brandon</td></tr>
  48. <tr><td>Benjamin</td></tr>
  49. </table>
  50. </div>
  51. <div>
  52. <table>
  53. <tr><td>Sam</td></tr>
  54. </table>
  55. </div>
  56. <div>
  57. <table>
  58. <tr><td>Glen</td></tr>
  59. <tr><td>Tane</td></tr>
  60. <tr><td>Ralph</td></tr>
  61. <tr><td>David</td></tr>
  62. <tr><td>Mike</td></tr>
  63. <tr><td>Dan</td></tr>
  64. </table>
  65. </div>
  66. <span>tr<span id="inner"></span></span>
  67. <script>
  68. $( "button" ).click(function() {
  69. var str = $( this ).text();
  70. $( "tr" ).css( "background", "white" );
  71. $( "tr" + str ).css( "background", "#ff0000" );
  72. $( "#inner" ).text( str );
  73. });
  74. </script>
  75. </body>
  76. </html>
运行一下


还没有评论.