Jquery-find filter区别

来源:互联网 发布:索尼as200 软件 编辑:程序博客网 时间:2024/06/05 00:37
find()会在div元素内 寻找 class为classname的元素。

filter()则是筛选div的class为classname的元素。

基本是find子元素找,filter是平级找

·find 函数是在当前对象集合的子元素中进行查询;
·filter 函数是对当前对象集合进行查询, 利用过滤条件缩小范围;
·find 函数的参数是 jQuery 选择器表达式;
·filter 的参数也是选择器表达式, 但可以有多个条件, 用逗号分隔(逻辑或关系);
·filter 的参数也可以是个函数, 调用函数时会自动传入 index 参数, 函数需返回 true或false 以选中或排除元素.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">      <head>          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">          <title>Document</title>          <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>          <script>          $(function(){              $('#btn1').click(function(){  <span style="white-space:pre"></span>   var   $s=$('div').find('.test');                alert($s.eq(1).html());              });              $('#btn2').click(function(){                  alert($('div').filter('.test').html());              });              $('#btn3').click(function(){                  alert($('div').filter('.last').html());              });              $('#btn4').click(function(){                  alert($('div').filter('.first,.last').html());              });                        });          </script>      </head>      <body>          <input type="button" value="test-find" id="btn1" />          <input type="button" value="test-filter" id="btn2" />          <input type="button" value="test-filter" id="btn3" />          <input type="button" value="test-filter" id="btn4" />                    <div class="first">first content<span class="test">test content</span></div>          <div class="last">last<span class="test">last test content</span></div>          <div class="last">last<span>last no test content</span></div>      </body>  </html>  



first contenttest content
lastlast test content
lastlast no test content
原创粉丝点击