Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of the hasClass () method

Shulou Source: shulou.com Published: 2022-06-01 07:34:39 10月24日 Update

Editor to share with you what is the use of the hasClass () method, I hope you will gain something after reading this article, let's discuss it together!

.hasClass (className)

Description: determines whether any matching element has a given (style) class assigned.

ClassName

Type: String

The name of the style to query.

The element may have a style name of. In HTML, use spaces to separate multiple styles:

If there is a specified style on the matching element, the .hasClass () method returns true, even though there may be other styles on the element. For example, adding the following code to the HTML above returns true:

$('# mydiv'). HasClass ('foo')

You can also:

$('# mydiv'). HasClass ('bar')

However, this returns false:

$('# mydiv'). HasClass ('quux')

Example:

Look for the 'selected' style on the matching elements.

P {margin: 8px; font-size:16px;}

.selected {color:red;}

This paragraph is black and is the first paragraph.

This paragraph is red and is the second paragraph.

First paragraph has selected class:

Second paragraph has selected class:

At least one paragraph has selected class:

$("div#result1") .append ($("p:first") .hasClass ("selected") .toString ()

$("div#result2") .append ($("p:last") .hasClass ("selected") .toString ()

("div#result3") .append ($("p") .hasClass ("selected") .toString ()

After reading this article, I believe you have a certain understanding of "what is the use of hasClass () method". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Styles elements methods examples articles above code multiple done more knowledge spaces types industry information information channels facets channels assignments queries Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Technology Apple MariaDB