:last Selects the last matched element. Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it. Additional Notes: Because :last is a jQuery extension and not part of the CSS specification, queries using :last cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :last to select elements, first select the elements using a pure CSS selector, then use.filter(":last").
:last-child:
Selects all elements that are the last child of their parent.
<div> <span>John,</span> <span>Karl,</span> <span>Brandon,</span> <span>Sam</span> </div> <div> <span>Glen,</span> <span>Tane,</span> <span>Ralph,</span> <span>David</span> </div> <script> $("div span:last-child") .css({color:"red", fontSize:"80%"}) .hover(function () { $(this).addClass("solast"); }, function () { $(this).removeClass("solast"); });
這兩個選擇器都是匹配集合中的最后一個元素,差別在于 :last 將匹配所有的集合中的最后一個元素。而 :last-child 將匹配集合中的所有位置為最后一個的子元素。:last 將永遠返回一個元素,而 :last-child可能返回一批元素。
$('div p:last') 選擇最后一個P元素并高亮顯示得出結(jié)果如下:
<div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div> <div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div> <div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div>
$('div p:last-child') 將選擇所有位于div最后一個p子元素,并高亮:
<div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div> <div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div> <div> <p>Paragraph</p> <p>Paragraph</p> <p>Paragraph</p> </div>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com