Home >Web Front-end >CSS Tutorial >How Can I Check if a jQuery Element Has a Specific CSS Class?
Question:
How can you ascertain if an element possesses a particular CSS class using jQuery?
Answer:
Leverage the hasClass method:
jQueryCollection.hasClass(className);
or
$(selector).hasClass(className);
Specify the class name as a string in the argument. This method returns a boolean value without supporting chaining.
Note: Whitespace in the specified class name will result in a literal match against the element's class string. For example, with the element:
<span>
$('span').hasClass('foo bar')
will return true, while:
$('span').hasClass('bar foo') $('span').hasClass('foo bar')
will return false.
The above is the detailed content of How Can I Check if a jQuery Element Has a Specific CSS Class?. For more information, please follow other related articles on the PHP Chinese website!