jQuery中的Grep和Filter有什么区别?
该grep()方法找到一个元素,然后该filter()方法返回匹配特定条件的元素。
jQuerygrep函数
示例
该grep()函数用于查找数组的元素。您可以尝试运行以下代码以了解如何使用grep(),
<html>
<head>
<title>jQuery grep() function</title>
<style>
div {
color: blue;
}
p {
color: red;
margin: 0;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div></div>
<p></p>
<script>
var arr1 = [ 1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2 ];
$( "div" ).text( arr1.join( ", " ) );
arr1 = jQuery.grep(arr1, function( n, i ) {
return ( n !== 5 && i > 6 );
});
$( "p" ).text( arr1.join( ", " ) );
</script>
</body>
</html>jQuery过滤器功能
jQueryfilter()方法将返回与特定条件匹配的元素。
示例
您可以尝试运行以下代码,以了解如何使用jQuery.filter()方法,
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").filter(".myclass").css("background-color", "blue");
});
</script>
</head>
<body>
<h1>Nhooo</h1>
<p class="myclass">Free Text Tutorials</p>
<p>Free Video Tutorials</p>
<p>Connecting Tutors</p>
</body>
</html>热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短