Examples of filter method usage in jquery

Source: Internet
Author: User

Examples of filter method usage in jquery

This article mainly introduces the usage of the filter method in jquery. The example analyzes the function of the filter method and compares the difference with the find method. For more information, see

 

 

This example describes the usage of the filter method in jquery. Share it with you for your reference. The specific analysis is as follows:

The filter () method reduces the set of matching elements to match the elements of the specified selector.
The parameter in the filter method can be a string value and contains a selector expression that matches the current element set.

1. filter parameter types can be divided into two types

1. Transfer Selector
$ ('A'). filter ('. external ')

2. Pass Filter Functions

The Code is as follows:

$ ('A'). filter (function (index ){
Return $ (this). hasClass ('external ');
})

 

Ii. Differences between find and filter in Jquery

1. find () searches for elements whose class is classname In the div element.
2. filter () is used to filter elements whose class is classname.
3. find the child element and filter the level.

4. The find function is used to query the child element of the current object set;
5. The filter function filters the current object set and uses filter conditions to narrow down the scope;
6. The parameters of the find function are jQuery selector expressions;

7. filter parameters are also selector expressions, but there can be multiple conditions separated by commas );
8. The filter parameter can also be a function. When a function is called, the index parameter is automatically passed in. The function must return true or false to select or exclude elements.

For example:

The Code is as follows:

<! DOCTYPE>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Document </title>
<Script>
$ (Function (){
$ ('# Btn1'). click (function (){
Alert((('div'hangzhou.find('.test'hangzhou.html ());
});
$ ('# Btn2'). click (function (){
Alert((('div'hangzhou.filter('.test'hangzhou.html ());
});
$ ('# Btn3'). click (function (){
Alert((('div'hangzhou.filter('.last'hangzhou.html ());
});
$ ('# Btn4'). click (function (){
Alert((('div'hangzhou.filter('.first,.last'hangzhou.html ());
});
});
</Script>
</Head>
<Body>
<Input type = "button" value = "test-find" id = "btn1"/>
<Input type = "button" value = "test-filter" id = "btn2"/>
<Input type = "button" value = "test-filter" id = "btn3"/>
<Input type = "button" value = "test-filter" id = "btn4"/>
<Div class = "first"> first content <span class = "test"> test content </span> </div>
<Div class = "last"> last <span class = "test"> last test content </span> </div>
<Div class = "last"> last <span> last no test content </span> </div>
</Body>
</Html>

 

I hope this article will help you with jQuery programming.

Related Article

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.