jQuery API 返回首页目录 | jQuery API 中英文对照版
filter(filter)

filter(filter)

从匹配的元素集合中删除那些不符合指定的筛选条件的元素(即返回与指定表达式匹配的元素集合)。这个方法用于缩小搜索的范围。

返回值:jQuery

参数:

  • filter (Function): 作为筛选条件的函数
 
示例:

删除所有拥有一个ol子元素的元素。

$("p").filter(function(index) { return $("ol",   this).length == 0; }) 

HTML 代码:

<p><ol><li>Hello</li></ol></p><p>How   are you?</p> 

结果:

[ <p>How are you?</p> ] 
 

filter( filter )

Removes all elements from the set of matched elements that do not pass the specified filter. This method is used to narrow down the results of a search.

Return value: jQuery
Parameters:

  • filter (Function): A function to use for filtering
 

Example:

Remove all elements that have a child ol element

 $("p").filter(function(index) {     return $("ol", this).length == 0;   })  
Before:
 <p><ol><li>Hello</li></ol></p><p>How are you?</p>  
Result:
 [ <p>How are you?</p> ]  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog