Example of filter () method usage in jQuery, jqueryfilter

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.