Latest web development tutorials

jQuery filter () method

jQuery traversal methods jQuery traversal methods

Examples

Returns all <p> element with the class name "intro" of:

$("p").filter(".intro")

try it"

Definition and Usage

filter () method returns the elements meet certain conditions.

This method allows you to specify a condition. Ineligible elements removed from the selection, qualifying element will be returned.

This method is usually used to narrow the search in the selected element is a combination of elements in the range.

Tip: filter () method is not () method relative.


grammar

$(selector).filter( criteria, function (index) )

参数 描述
criteria 可选。规定要从被选元素组合中返回的选择器表达式、jQuery 对象、一个或多个元素。

提示:如需规定多个条件,请使用逗号分隔。
function( index ) 可选。为集合中的每个元素规定要运行的函数。如果返回 true,则保留元素,否则元素将被移除。
  • index- 集合中元素的 index 位置。
注意: this 是当前的 DOM 元素。

Examples

More examples

Back to all even-numbered <p> element
Use: even select and filter () to return a <p> element in all even-numbered.

Multiple conditions
How to return to class as "intro" and id is "outro" all <p> elements.

Use jQuery object
How to Return <div> element within the class of "intro" all <p> elements via jQuery object.

DOM
How to return the DOM element with id "intro" of a <p> element.

Using Functions
How to select the function has two <span> element in all <p> elements therein.


jQuery traversal methods jQuery traversal methods