jQuery filter() method
Example
Return all with class name "intro"
element:
$("p").filter(".intro")
Try it »
Definition and Usage
The filter() method returns elements that match a certain condition.
This method lets you specify a condition. Elements that do not match the condition are removed from the selection, and elements that match are returned.
This method is often used to narrow the scope of the search for elements within a set of selected elements.
**Tip:** The filter() method is the opposite of not() method.
Syntax
$(
selector
).filter(
criteria,
function
(index)
)
| Parameters | Description |
|---|---|
| criteria | Optional. Specifies a selector expression, jQuery object, or one or more elements to return from the set of selected elements. |
**提示:**如需规定多个条件,请使用逗号分隔。 |
| function(index) | Optional. Specifies a function to run for each element in the set. If it returns true, the element is kept; otherwise the element is removed.
- index - the index position of the element in the set.
More examples
Element
Use the :even selector and filter() to return all even-numbered
.
Multiple conditions
How to return all with class "intro" and id "outro"
.
Using a jQuery object
How to return via a jQuery object
.
DOM
How to return one with id "intro" via a DOM element
.
Using the function
How to use a function to select those having two all having two elements inside them
.
jQuery Traversal Methods