:last-of-type
选择的所有元素之间具有相同元素名称的最后一个兄弟元素。
jQuery(":last-of-type")
:last-of-type选择器匹配在文档树中具有相同的父元素并且相同的元素名称,后面没有任何其他元素的元素。
例子
在每一个匹配的div中找到最后一个span,并加上 CSS 以及增加鼠标悬停效果。
<!DOCTYPE html>
<html>
<head>
<style>
span.solast { text-decoration:line-through; }
</style>
<script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script>
</head>
<body>
<div>
<span>Corey,</span>
<span>Yehuda,</span>
<span>Adam,</span>
<span>Todd</span>
</div>
<div>
<span>Jörn,</span>
<span>Scott,</span>
<span>Timo,</span>
<b>Nobody</b>
</div>
<script>
$("span:last-of-type")
.css({color:"red", fontSize:"80%"})
.hover(function () {
$(this ).addClass("solast");
}, function () {
$(this ).removeClass("solast");
});
</script>
</body>
</html>
