Home >Web Front-end >CSS Tutorial >How Can I Check if a jQuery Element Has a Specific CSS Class?

How Can I Check if a jQuery Element Has a Specific CSS Class?

Barbara Streisand
Barbara StreisandOriginal
2024-11-24 01:12:13545browse

How Can I Check if a jQuery Element Has a Specific CSS Class?

Determining CSS Class Presence with jQuery

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!

Statement:
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