jQuery中find()方法和filter()方法的区别

jQuery官方的API这样说明filterfind函数:

     filter(selector):
    Description: Reduce the set of matched elements to those that match the selector or pass the function’s test.

    find(selector):
    Description: Get the descendants of each element in the current set of matched elements, filtered by a selector. 

find()会在当前指定元素中查找符合条件的子元素,是对它的子集操作,而filter()则是在当前指定的元素集合中查找符合条件的元素,是对自身集合元素进行筛选。

看下边的例子就会一目了然:

HTML代码:

<div class="benben">
	<p>Hello,World!</p>
	<p>Hello,World Again!</p>
	<p class="test">Test1</p>
</div>
<div class="test">
	<p>Test2</p>
</div>

jQuery代码:

<script type="text/javascript">
//using find()
var $find=$("div").find(".test");
alert($find.html());//display "Test1"
//using test()
var $filter=$("div").filter(".test");
alert($filter.html());//display "Test2"
</script>

很多时候经常用到find()或者filter(),下边的代码中就用到了find()方法在指定元素中查找符合条件的子元素。

<script type="text/javascript">
$(document).ready(function() {
	//mouse hover
	$("ul.test>li").hover(function() {
		$(this).find("a:first").css({
			"background":"white",
			"position":"relative"
		});
	},
	//mouse out
	function(){
		$(this).find("a:first").css({
			"background":"",
			"position":""
		});
	});
});
</script>

参考:http://www.benben.cc/blog/?p=352


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值