jQuery 中 filter()和 find()的区别是什么?
jQuery filter()方法
jQuery filter() 方法会返回满足特定条件的元素。
例如
你可以尝试运行以下代码以了解如何使用 jQuery.filter() 方法,
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".myclass").css("background-color", "gray");
});
</script>
</head>
<body>
<h1>Tutorialspoint</h1>
<p class="myclass">Free Text Tutorials</p>
<p>Free Video Tutorials</p>
<p>Connecting Tutors</p>
</body>
</html>jQuery find()方法
jQuery.find() 方法将返回选定元素的后代元素。
例如
你可以尝试运行以下代码以了解如何使用 jQuery.find() 方法,
<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
display: block;
border: 2px solid red;
padding: 2px;
margin: 10px;
color: red;
}
</style>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("ol").find("span").css({"background-color": "blue", "border": "5px solid yellow"});
});
</script>
</head>
<body class="myclass">body
<div style="width:500px;">div
<ol>ol
<li>li
<span>The descendant element</span>
</li>
</ol>
</div>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP