返回值:jqueryfilter(expr|obj|ele|fn)
概述
筛选出与指定表达式匹配的元素集合。
这个方法用于缩小匹配的范围。用逗号分隔多个表达式
参数
exprstringv1.0
字符串值,包含供匹配当前元素集合的选择器表达式。
jquery objectobjectv1.0
现有的jquery对象,以匹配当前的元素。
element expressionv1.4
一个用于匹配元素的dom元素。
function(index) functionv1.4
一个函数用来作为测试元素的集合。它接受一个参数index,这是元素在jquery集合的索引。在函数, this指的是当前的dom元素。
示例
参数selector描述:
保留带有select类的元素
html 代码:
<p>hello</p><p>hello again</p><p class="selected">and again</p>
jquery 代码:
$("p").filter(".selected")
结果:
[ <p class="selected">and again</p> ]
参数selector描述:
保留第一个以及带有select类的元素
html 代码:
<p>hello</p><p>hello again</p><p class="selected">and again</p>
jquery 代码:
$("p").filter(".selected, :first")
结果:
[ <p>hello</p>, <p class="selected">and again</p> ]
回调函数 描述:
保留子元素中不含有ol的元素。
html 代码:
<p><ol><li>hello</li></ol></p><p>how are you?</p>
jquery 代码:
$("p").filter(function(index) {
return $("ol", this).length == 0;
});
结果:
[ <p>how are you?</p> ]