|
| 1 | +<?xml version="1.0"?> |
| 2 | +<entry type="selector" name="nth-last-of-type" return=""> |
| 3 | + <title>:nth-last-of-type() Selector</title> |
| 4 | + <sample>:nth-last-of-type(index/even/odd/equation)</sample> |
| 5 | + <signature> |
| 6 | + <added>1.9</added> |
| 7 | + <argument name="index" type="Number/String"> |
| 8 | + <desc>The index of each child to match, starting with the last one (<code>1</code>), the string <code>even</code> or <code>odd</code>, or an equation ( eg. <code>:nth-last-of-type(even)</code>, <code>:nth-last-of-type(4n)</code> )</desc> |
| 9 | + </argument> |
| 10 | + </signature> |
| 11 | + <desc>Selects all elements that are the nth-child of their parent, counting from the last element to the first.</desc> |
| 12 | + <longdesc> |
| 13 | + <p>Because jQuery's implementation of <code>:nth-</code> selectors is strictly derived from the CSS specification, the value of <code>n</code> is "1-indexed", meaning that the counting starts at 1. For other selector expressions such as <code>:eq()</code> or <code>:even</code> jQuery follows JavaScript's "0-indexed" counting. Given a single <code><ul></code> containing three <code><li></code>s, <code>$('li:nth-last-of-type(1)')</code> selects the third, last, <code><li></code>.</p> |
| 14 | + <p>Further discussion of this usage can be found in the <a href="http://www.w3.org/TR/css3-selectors/#nth-last-of-type-pseudo">W3C CSS specification</a>.</p> |
| 15 | + </longdesc> |
| 16 | + <example> |
| 17 | + <desc>Find the second to last li in each matched ul and note it.</desc> |
| 18 | + <code><![CDATA[$("ul li:nth-last-of-type(2)").append("<span> - 2nd to last!</span>");]]></code> |
| 19 | + <css><![CDATA[ |
| 20 | + div { float:left; } |
| 21 | + span { color:blue; } |
| 22 | + ]]></css> |
| 23 | + <html><![CDATA[<div> |
| 24 | + <ul> |
| 25 | + <li>John</li> |
| 26 | + <li>Karl</li> |
| 27 | + <li>Adam</li> |
| 28 | + </ul> |
| 29 | +</div> |
| 30 | +<div> |
| 31 | + <ul> |
| 32 | + <li>Dan</li> |
| 33 | + </ul> |
| 34 | +</div> |
| 35 | +<div> |
| 36 | + <ul> |
| 37 | + <li>Dave</li> |
| 38 | + <li>Rick</li> |
| 39 | + <li>Timmy</li> |
| 40 | + <li>Gibson</li> |
| 41 | + </ul> |
| 42 | +</div>]]></html> |
| 43 | + </example> |
| 44 | + <example> |
| 45 | + <desc>This is a playground to see how the selector works with different strings. </desc> |
| 46 | + <code><![CDATA[ |
| 47 | +$("button").click(function () { |
| 48 | + var str = $(this).text(); |
| 49 | + $("tr").css("background", "white"); |
| 50 | + $("tr" + str).css("background", "#ff0000"); |
| 51 | + $("#inner").text(str); |
| 52 | +}); |
| 53 | +]]></code> |
| 54 | + <css><![CDATA[ |
| 55 | +button { display:block; font-size:12px; width:100px; } |
| 56 | +div { float:left; margin:10px; font-size:10px; |
| 57 | + border:1px solid black; } |
| 58 | +span { color:blue; font-size:18px; } |
| 59 | +#inner { color:red; } |
| 60 | +td { width:50px; text-align:center; } |
| 61 | +]]></css> |
| 62 | + <html><![CDATA[<div> |
| 63 | + <button>:nth-last-of-type(even)</button> |
| 64 | + <button>:nth-last-of-type(odd)</button> |
| 65 | + <button>:nth-last-of-type(3n)</button> |
| 66 | + <button>:nth-last-of-type(2)</button> |
| 67 | +</div> |
| 68 | +<div> |
| 69 | + <button>:nth-last-of-type(3n+1)</button> |
| 70 | + <button>:nth-last-of-type(3n+2)</button> |
| 71 | +</div> |
| 72 | +
|
| 73 | +<div> |
| 74 | + <table> |
| 75 | + <tr><td>John</td></tr> |
| 76 | + <tr><td>Karl</td></tr> |
| 77 | + <tr><td>Brandon</td></tr> |
| 78 | + <tr><td>Benjamin</td></tr> |
| 79 | + </table> |
| 80 | +</div> |
| 81 | +<div> |
| 82 | + <table> |
| 83 | + <tr><td>Sam</td></tr> |
| 84 | + </table> |
| 85 | +</div> |
| 86 | +<div> |
| 87 | + <table> |
| 88 | + <tr><td>Glen</td></tr> |
| 89 | + <tr><td>Tane</td></tr> |
| 90 | + <tr><td>Ralph</td></tr> |
| 91 | + <tr><td>David</td></tr> |
| 92 | + <tr><td>Mike</td></tr> |
| 93 | + <tr><td>Dan</td></tr> |
| 94 | + </table> |
| 95 | +</div> |
| 96 | +
|
| 97 | +<span>tr<span id="inner"></span></span> |
| 98 | +]]></html> |
| 99 | + </example> |
| 100 | + <category slug="selectors/child-filter-selectors"/> |
| 101 | + <category slug="version/1.9"/> |
| 102 | +</entry> |
0 commit comments