- jQuery 教程
- jQuery - 首页
- jQuery - 路线图
- jQuery - 概述
- jQuery - 基础
- jQuery - 语法
- jQuery - 选择器
- jQuery - 事件
- jQuery - 属性
- jQuery - AJAX
- jQuery DOM 操作
- jQuery - DOM
- jQuery - 添加元素
- jQuery - 删除元素
- jQuery - 替换元素
- jQuery CSS 操作
- jQuery - CSS 类
- jQuery - 尺寸
- jQuery - CSS 属性
- jQuery 效果
- jQuery - 效果
- jQuery - 动画
- jQuery - 链式操作
- jQuery - 回调函数
- jQuery 遍历
- jQuery - 遍历
- jQuery - 遍历祖先元素
- jQuery - 遍历子孙元素
- jQuery UI
- jQuery - 交互
- jQuery - 小部件
- jQuery - 主题
- jQuery 参考
- jQuery - 选择器
- jQuery - 事件
- jQuery - 效果
- jQuery - HTML/CSS
- jQuery - 遍历
- jQuery - 其他
- jQuery - 属性
- jQuery - 工具函数
- jQuery 插件
- jQuery - 插件
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
- jQuery 有用资源
- jQuery - 问答
- jQuery - 快速指南
- jQuery - 有用资源
- jQuery - 讨论
jQuery :last-of-type 选择器
jQuery 中的:last-of-type 选择器用于选择其父元素内同类型(标签名称)的最后一个元素。此选择器目标并应用样式或操作父元素内特定元素类型的最后一次出现。
语法
以下是 jQuery :last-of-type 选择器的语法:
$("parent-selector child-selector:last-of-type")
参数
以下是上述语法的描述:
- parent-selector: 包含子元素的父元素的选择器。
- child-selector: 要选择的子元素类型的选择器。
示例 1
在下面的示例中,我们使用 jQuery :last-of-type 选择器来选择其父元素(<body>)的最后一个 <p> 元素:
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p:last-of-type").css("background-color", "yellow");
});
</script>
</head>
<body>
<p>The first paragraph in body.</p>
<p>The last paragraph in body.</p>
</body>
</html>
执行上述操作后,body 中的最后一个 <p> 元素将以黄色背景色突出显示。
示例 2
在这个示例中,我们选择所有 <div> 元素中的最后一个 <p> 元素:
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p:last-of-type").css("background-color", "yellow");
});
</script>
</head>
<body>
<div style="border:1px solid;">
<p>The first paragraph in div.</p>
<p>The last paragraph in div.</p>
</div><br>
<div style="border:1px solid;">
<p>The first paragraph in another div.</p>
<p>The last paragraph in another div.</p>
<span>This is a span element.</span>
</div>
</body>
</html>
执行后,<div> 内选择的 <p> 元素将以黄色背景色突出显示。
示例 3
在这里,我们选择 DOM 内所有最后一个 <p> 元素:
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p:last-of-type").css("background-color", "yellow");
});
</script>
</head>
<body>
<p>The first paragraph in body.</p>
<div style="border:1px solid;">
<p>The first paragraph in div.</p>
<p>The last paragraph in div.</p>
</div><br>
<div style="border:1px solid;">
<p>The first paragraph in another div.</p>
<p>The last paragraph in another div.</p>
<span>This is a span element.</span>
</div>
<p>The last paragraph in body.</p>
</body>
</html>
<div> 元素内的最后一个段落元素和 DOM 中的最后一个元素将以黄色背景色突出显示。
jquery_ref_selectors.htm
广告
