Examples of filter method usage in jquery _ jquery-js tutorial

Source: Internet
Author: User
This article mainly introduces the usage of the filter method in jquery. The example analyzes the functions of the filter method and compares the differences with the find method, for more information about how to use the filter method in jquery, see the following example. 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 p element.
2. filter () is the element that filters p's class as 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:





Document
Script
$ (Function (){
$ ('# Btn1'). click (function (){
Alert((('p'hangzhou.find('.test'hangzhou.html ());
});
$ ('# Btn2'). click (function (){
Alert((('p'hangzhou.filter('.test'hangzhou.html ());
});
$ ('# Btn3'). click (function (){
Alert((('p'hangzhou.filter('.last'hangzhou.html ());
});
$ ('# Btn4'). click (function (){
Alert((('p'hangzhou.filter('.first,.last'hangzhou.html ());
});
});
Script






First contenttest content


Lastlast test content


Lastlast no test content



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.