.last()
返回: jQuery
.last()
描述:把匹配的元素集合缩小到集合中的最后一个。
加入于: 1.4
.last()该方法不接受任何参数
给定一个jQuery对象,代表了一个DOM元素的集合,.last()方法从集合中最后一个元素构造了一个新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>
我们可以将这个方法应用到列表项目的集合上:
$( "li" ).last().css( "background-color", "red" );
这个调用的结果是最后一项变成红色背景:
示例
高亮段落文本中最后一个<span>。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>last demo</title>
<style>
.highlight {
background-color: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.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>
演示结果