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

Barbara Streisand
Release: 2024-11-24 01:12:13
Original
477 people have browsed it

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);
Copy after login

or

$(selector).hasClass(className);
Copy after login

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>
Copy after login
$('span').hasClass('foo bar')
Copy after login

will return true, while:

$('span').hasClass('bar foo')
$('span').hasClass('foo  bar')
Copy after login

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!

source:php.cn
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 Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template