<html lang="en"> <head> <meta charset="utf-8"> <title>jQuery.grep demo</title> <style> div { color: blue; } p { color: green; margin: 0; } span { color: red; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div></div> <p></p> <span></span> <a href=""></a> <script> var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ]; $( "div" ).text( arr.join( ", " ) );//1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 arr = jQuery.grep(arr, function( n, i ) { return ( n !== 5 && i > 4 ); }); $( "p" ).text( arr.join( ", " ) );//1, 9, 4, 7, 3, 8, 6, 9, 1 arr = jQuery.grep(arr, function(n,i) { return true; }); $( "span" ).text( arr.join( ", " ) );//1, 9, 4, 7, 3, 8, 6, 9, 1 arr=jQuery.grep(arr,function(n,i){ return false; }); $( "a" ).text( arr.join( ", " ) );//空 </script> </body> </html>
grep过滤
最新推荐文章于 2022-07-30 22:48:51 发布