设为首页
 加入收藏
 联系站长
站内搜索:   您的位置:PHP自学网>> 首页 >> Javascript >> jquery中find与filter函数的用法区别

jquery中find与filter函数的用法区别

日期:2010-05-15     来自:互联网    浏览:3824
Jquery中find与filter函数的用法区别:
区别:
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=gb2312">
<title>Jquery中find与filter函数的用法区别-php自学网</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script>
$(function(){
$('#btn1').click(function(){
alert($('div').find('.test').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>
  TAGs: jquery   find   filter  
文章评论:(查看全部评论)
昵   称: 匿名 关闭留言
还可以输入250个字 (请自觉遵守互联网相关政策法规)

验证码: 点击图片刷新验证码
相关文章
热门文章
随机文章