Home > Web Front-end > Front-end Q&A > How to determine whether an element has a class in jquery

How to determine whether an element has a class in jquery

藏色散人
Release: 2023-03-21 10:47:38
Original
3101 people have browsed it

Jquery method to determine whether an element has a class: 1. Determine whether an element has a certain class through the "hasClass('classname')" method; 2. Determine whether an element has a certain class through the "is('.classname')" method Whether it has a certain class.

How to determine whether an element has a class in jquery

The operating environment of this tutorial: Windows 10 system, jquery3.6 version, Dell G3 computer.

How does jquery determine whether an element has a class?

jquery determines whether an element has a certain class

Two methods are as follows:

1, hasClass('classname')

2, is('.classname')

Example:

1. Use is('.classname') method

$('div').is('.redColor')
Copy after login

2. Use hasClass('classname') Method (note that the lower version of jquery may be hasClass('.classname'))

$('div').hasClass('redColor')
Copy after login

How to determine whether an element has a class in jquery

Extended information:

About the hasclass( of jquery that appears above) ) and is() Introduction

1, is()

is(expr|obj|ele|fn)
Copy after login

Detects a set of matching elements based on a selector, DOM element or jQuery object, if at least one element matches the given The expression returns true.

If no element matches, or the expression is invalid, 'false' is returned. '''Note:''''Support for all expressions is only available in jQuery 1.3. In previous versions, if complex expressions, such as hierarchical selectors (for example, ~ and > ), were provided, they would always return true

2. hasclass()

checks the current Whether the element contains a specific class, if so, returns true.

This is actually is("." class).

Description: Perform an animation on an element containing a certain class.

Recommended learning: jQuery video tutorial

The above is the detailed content of How to determine whether an element has a class in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template