Home > Web Front-end > JS Tutorial > Example of usage of filter() method in jQuery_jquery

Example of usage of filter() method in jQuery_jquery

WBOY
Release: 2016-05-16 16:22:03
Original
1217 people have browsed it

This method can filter out elements or a collection of elements that match the specified expression or method return value.

Grammar structure 1:
Filter out the set of elements that match the specified expression.

Copy code The code is as follows:
$(selector).filter(expr)

Parameter list:

参数 描述
expr 字符串值,用于筛选当前元素集合的选择器表达式。

Example code:

Copy code The code is as follows:






filter() function-Script Home






  • HTML Zone

  • Javascript Zone

  • Div Css Zone

  • Jquery Zone





This code can set the font color in the li with the class name js to blue.

Grammar structure 2:

Filter out the specified jquery object from the element collection.

Copy code The code is as follows:
$(selector).filter(element)

Parameter list:

参数 描述
element 用于匹配当前元素集合中元素的jQuery对象或者DOM对象。

Example code:

Example 1:

Copy code The code is as follows:






filter() function-Script Home






  • HTML Zone

  • Javascript Zone

  • Div Css Zone

  • Jquery Zone





Example 2:

Copy code The code is as follows:






filter() function-Script Home






  • HTML Zone

  • Javascript Zone

  • Div Css Zone

  • Jquery Zone





Grammar structure three:

Filter out the set of elements that match the return value of the specified function.
The filter() method will be calculated once for each object internally (such as $.each). If the called function returns false then the element is deleted, otherwise it is retained.

Copy code The code is as follows:
$(selector).filter(function())

Parameter list:

参数 描述
function(index) 定义返回filter()匹配值的函数。
index为当前元素在匹配元素集合中的索引值。

Example code:

Copy code The code is as follows:






filter() function-Script Home






  • HTML Zone

  • Javascript Zone

  • Div Css Zone

  • Jquery Zone



  • The font is going to turn blue





I hope this article will be helpful to everyone’s jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template