.last()
.last() 返回: jQuery
描述: 獲取元素集合中最后一個元素。
version added: 1.4.last()
如果一個jQuery對象表示一個DOM元素的集合,.last()方法從最后一個匹配元素中構(gòu)造一個新的jQuery對象。
考慮一個頁面上的一個簡單的列表:
<ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
我們可以應(yīng)用此方法設(shè)置列表項:
$('li').last().css('background-color', 'red');
調(diào)用的結(jié)果是最后一個項目為紅色背景。
Example:
Highlight the last span in a paragraph.
<!DOCTYPE html>
<html>
<head>
<style>.highlight{background-color: yellow}</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p><span>Look:</span> <span>This is some text in a paragraph.</span> <span>This is a note about it.</span></p>
<script>$("p span").last().addClass('highlight');</script>
</body>
</html>