Home > Article > Web Front-end > How to determine whether a class exists in jquery
How to determine whether a class exists in jquery: 1. Use the [is('.classname')] method; 2. Use the [hasClass('classname')] method, the code is [$('div' ).hasClass('redColor')].
The operating environment of this tutorial: windows7 system, jquery3.2.1 version. This method is suitable for all brands of computers.
Methods for whether class exists in jquery:
There are two methods you can use in jquery to determine whether an element contains a certain class (class). Both methods have the same functionality. The 2 methods are as follows:
is('.classname')
hasClass('classname')
The following is an example of whether a div element contains a redColor:
1. Use the 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')
The following is an example of detecting whether an element contains a redColor class, When included, change its class to blueColor.
.redColor { background:red; } .blueColor { background:blue; } jQuery check if an element has a certain class
This is a div tag with class name of "redColor"
Related free learning recommendations: JavaScript (video)
The above is the detailed content of How to determine whether a class exists in jquery. For more information, please follow other related articles on the PHP Chinese website!