Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of the hasClass () method

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report