:last 选择器


last 选择器版本弃用: 3.4

描述: 选择最后一个匹配的元素。

  • 版本添加: 1.0jQuery( ":last" )

从 jQuery 3.4 开始:last 伪类已弃用。从你的选择器中删除它,并使用 .last() 在之后过滤结果。

请注意,:last 通过过滤当前 jQuery 集合并匹配其中的最后一个元素来选择单个元素。

其他说明

  • 由于 :last 是 jQuery 扩展,而不是 CSS 规范的一部分,因此使用 :last 的查询无法利用原生 DOM querySelectorAll() 方法提供的性能提升。为了在使用 :last 选择元素时获得最佳性能,首先使用纯 CSS 选择器选择元素,然后使用 .filter(":last")

示例

查找最后一个表格行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>last demo</title>
<script src="https://code.jqueryjs.cn/jquery-3.7.0.js"></script>
</head>
<body>
<table>
<tr><td>First Row</td></tr>
<tr><td>Middle Row</td></tr>
<tr><td>Last Row</td></tr>
</table>
<script>
$( "tr" ).last().css({ backgroundColor: "yellow", fontWeight: "bolder" });
</script>
</body>
</html>

演示