Home >Web Front-end >Front-end Q&A >How to determine whether an element has a class in jquery
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.
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')
2. Use hasClass('classname') Method (note that the lower version of jquery may be hasClass('.classname'))
$('div').hasClass('redColor')
Extended information:
About the hasclass( of jquery that appears above) ) and is() Introduction
1, is()
is(expr|obj|ele|fn)
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!