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

filter(expression)

从所有匹配的元素集合中删除那些与指定的表达式(可以是多个)不匹配的元素(即返回与指定表达式匹配的元素集合)。这个方法用于缩小匹配的范围。

使用表达式字符串数组可以实现一次完成多重筛选的效果。

返回值:jQuery

参数:

  • expression (String|Array<String>): 要搜索的表达式(或表达式数组)
 
示例:

选择所有段落并删除那些类名不是selected的元素。

$("p").filter(".selected") 

HTML 代码:

<p class="selected">Hello</p><p>How are   you?</p>

结果:

[ <p class="selected">Hello</p> ] 
示例:

选择所有段落并删除那些类名不是selected的元素,但不删除第一个元素。

$("p").filter([".selected", ":first"]) 

HTML 代码:

<p>Hello</p><p>Hello   Again</p><p class="selected">And Again</p>

结果:

[ <p>Hello</p>, <p class="selected">And   Again</p> ] 
 
filter( expression )

Removes all elements from the set of matched elements that do not match the specified expression(s). This method is used to narrow down the results of a search.

Provide a comma-separated list of expressions to apply multiple filters at once.

Return value: jQuery
Parameters:

  • expression (String): Expression(s) to search with.
 

Example:

Selects all paragraphs and removes those without a class "selected".

 $("p").filter(".selected")  
Before:
 <p class="selected">Hello</p><p>How are you?</p>  
Result:
 [ <p class="selected">Hello</p> ]  

Example:

Selects all paragraphs and removes those without class "selected" and being the first one.

 $("p").filter(".selected,?:first")  
Before:
 <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>  
Result:
 [ <p>Hello</p>, <p class="selected">And Again</p> ]  


 

相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog