Example of filter () method usage in jQuery, jqueryfilter
This method can filter out elements or element sets that match the returned values of a specified expression or method.
Syntax structure 1:
Filters out a set of elements that match the specified expression.
Copy codeThe Code is as follows: $ (selector). filter (expr)
Parameter List:
| Parameters |
Description |
| Expr |
String Value, used to filter the selector expression of the current element set. |
Instance code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> filter () function-helper's house </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Li"). filter (". js" ).css ("color", "blue ")
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> HTML zone </li>
<Li class = "js"> Javascript area </li>
<Li> Div + Css area </li>
<Li> Jquery area </li>
</Ul>
</Div>
</Body>
</Html>
This Code sets the font color of the class li named js to blue.
Syntax structure 2:
Filter the specified jquery object from the element set.
Copy codeThe Code is as follows: $ (selector). filter (element)
Parameter List:
| Parameters |
Description |
| Element |
The jQuery object or DOM object used to match the elements in the current element set. |
Instance code:
Instance 1:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> filter () function-helper's house </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Li"). filter (document. getElementById ("js" )).css ("color", "blue ")
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> HTML zone </li>
<Li id = "js"> Javascript area </li>
<Li> Div + Css area </li>
<Li> Jquery area </li>
</Ul>
</Div>
</Body>
</Html>
Example 2:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> filter () function-helper's house </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Li"). filter ($ ("li" has 0]00000000.css ("color", "blue ")
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> HTML zone </li>
<Li id = "js"> Javascript area </li>
<Li> Div + Css area </li>
<Li> Jquery area </li>
</Ul>
</Div>
</Body>
</Html>
Syntax structure 3:
Filters out a set of elements that match the returned values of the specified function.
The filter () method calculates each object once (for example, $. each ).. If the call function returns false, the element is deleted; otherwise, the element is retained.
Copy codeThe Code is as follows: $ (selector). filter (function ())
Parameter List:
| Parameters |
Description |
| Function (index) |
Defines the function that returns the matching value of filter. Index is the index value of the current element in the Matching Element Set. |
Instance code:
Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> filter () function-helper's house </title>
<Script type = "text/javascript" src = "mytest/jQuery/jquery-1.8.3.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("Ul"). filter (function (index ){
Return $ (". js", this). length = 0;
}).Css ("color", "blue ")
})
</Script>
</Head>
<Body>
<Div>
<Ul>
<Li> HTML zone </li>
<Li class = "js"> Javascript area </li>
<Li> Div + Css area </li>
<Li> Jquery area </li>
</Ul>
<Ul>
<Li> the font needs to change to Blue </li>
</Ul>
</Div>
</Body>
</Html>
I hope this article will help you with jQuery programming.