jQuery中find()方法和filter()方法的区别
2013-09-04 17:47
836 查看
jQuery官方的API这样说明filter和find函数:
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
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
相关文章推荐
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- [jq]jquery的has,find和filter方法的区别
- jQuery中find()方法、children方法、filter()方法的区别分析
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- 解说jquery的has()方法以及与find()方法以及filter()方法的区别
- jQuery源码分析之实例find和filter方法的区别七问
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- 一个jquery的面试题(find()和filter方法有何区别)
- Jquery中的has、find、filter方法区别
- Jquery中的has、find、filter方法区别
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- jquery方法find和filter的区别
- 浅谈jQuery中find()与filter()两种方法联系与区别
- jQuery中find和filter的区别
- jQuery的find()和children()搜索后代方法用法和区别
- jQuery初学:find()方法及children方法的区别分析
- 基于jquery的has()方法以及与find()方法以及filter()方法的区别详解
- jquery--find与children方法的区别