What does jquery use for filtering?

WBOY
Release: 2022-05-10 15:09:03
Original
1971 people have browsed it

Filtering method: 1. Use ".hasClass("class name")" to filter class names; 2. Use ".eq(n)" to filter subscripts; 3. Use ".is(select (selector)" filter judgment; 4. Use ".not (selector or function)" to reverse filter; 5. Use ".filter (selector or function)" to filter expressions.

What does jquery use for filtering?

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

What to use for jquery filtering

In jQuery, there are 5 common filtering methods:

1. Class name filtering: hasClass( );

Class name filtering refers to filtering based on the class of the element. In jQuery, we can use the hasClass() method to implement class name filtering.

Syntax:

$().hasClass("类名")
Copy after login

hasClass() method is generally used to determine whether the element contains the specified class name: if it does, it returns true; if it does not, it returns false.

2. Subscript filtering: eq();

Subscript filtering refers to filtering based on the subscript of the element collection. In jQuery, we can use the eq() method to implement subscript filtering.

Syntax:

$().eq(n)
Copy after login

n is an integer. When n takes the value 0 or a positive integer, eq(0) obtains the 1st element, eq(1) obtains the 2nd element,..., and so on. When n is a negative integer, eq(-1) obtains the first element from the last, eq(-2) obtains the second element from the last,..., and so on.

3. Judgment filtering: is();

Judgment filtering refers to judging based on certain conditions, and then selecting those that meet the conditions Elements. In jQuery, we can use the is() method to implement judgment filtering.

Syntax:

$().is(selector)
Copy after login

Parameter selector is a selector. The is() method is used to determine whether there is an element that meets the conditions in the currently selected element collection: if it exists, it returns true; if it does not exist, it returns false.

The is() method is very easy to use, and whether it can be used well directly determines whether your code is efficient. When developing with jQuery, there is nothing you can't do, only things you can't imagine. Listed below are the common function codes of the is() method:

//判断元素是否可见
$().is(":visible")
//判断元素是否处于动画中
$().is(":animated")
//判断单选框或复选框是否被选中
$().is(":checked")
//判断当前元素是否为第一个子元素
$(this).is(":first-child")
//判断文本中是否包含jQuery这个词
$().is(":contains('jQuery')")
//判断是否包含某些类名
$().is(".select")
Copy after login

4. Reverse filtering: not();

in In jQuery, we can also use the not() method to filter elements that "do not meet the conditions" and return the remaining elements that meet the conditions. Among them, the not() method can use a selector to filter or a function to filter.

Syntax:

$().not(selector或fn)
Copy after login

When the not() method parameter is a selector, it means using the selector to filter elements that do not meet the conditions, and then select the remaining elements. When the parameter of the not() method is a function, it means using the function to filter elements that do not meet the conditions, and then select the remaining elements.

5. Expression filtering: filter(), has().

Expression filtering refers to using "custom expression" to select elements that meet the conditions. This custom expression can be a selector or a function.

In jQuery, there are two methods for expression filtering: one is the filter() method and the other is the has() method.

jQuery filter() method

In jQuery, the filter() method is a very powerful filtering method. It can use selectors to filter, or you can use function to filter.

1) Selector filtering

Selector filtering refers to using a selector to select elements that meet conditions.

Syntax:

$().filter(selector)
Copy after login

Parameter selector is a selector.

2) Function filtering

Function filtering refers to selecting elements that meet the conditions based on the return value of the function.

Syntax:

$().filter(fn)
Copy after login

The parameter fn is a callback function.

The filter() method is very powerful and includes almost all the functions of the filtering methods learned before. However, it is precisely because there are too many things encapsulated inside the filter() method that it runs very slowly. Therefore, in actual development, it is recommended that you give priority to other filtering methods and use the filter() method as a last resort.

jQuery has() method

In jQuery, in addition to using the filter() method for expression filtering, we can also use the has() method. Although the has() method is not as powerful as the filter() method, it runs faster.

Syntax:

$().has(selector)
Copy after login

Parameter selector is a selector.

The has() method has similar functions to the filter() method, but the has() method can only use selectors to filter, not functions. Therefore we can think of the has() method as a streamlined version of the filter() method.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of What does jquery use for filtering?. For more information, please follow other related articles on the PHP Chinese website!

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