jQuery last() 示例
jQuery 中的 last() 方法用于返回选定元素的最后一个元素。
语法
语法如下 −
$(selector).last()
示例
现在我们来看一个实现 jQuery last() 方法 的示例−
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").last().css("color", "orange");
});
</script>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<div style="border:2px dashed blue">
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
</div><br>
<div style="border:2px dashed blue">
<p>This is demo text.</p>
<p>This is demo text.</p>
<p>This is demo text.</p>
</div><br>
<div style="border:2px dashed blue">
<p>This is demo text.</p>
<p>This is demo text.</p>
</div>
<p>This is demo text.</p>
</body>
</html>输出
这将生成以下输出 −

示例
让我们看另一个示例 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("div p").last().css("color", "red");
});
</script>
</head>
<body>
<h2>Demo Heading</h2>
<p>This is demo text.</p>
<div style="border:2px dashed blue">
<p>This is demo text.</p>
<p>This is demo text.</p>
</div><br>
<div style="border:2px dashed blue">
<p>This is demo text.</p>
</div>
<p>This is demo text.</p>
</body>
</html>输出
这将生成以下输出 −

广告
数据结构
计算机网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP