Home  >  Article  >  Web Front-end  >  How to get the value of class with jQuery

How to get the value of class with jQuery

青灯夜游
青灯夜游Original
2020-12-18 17:43:259598browse

In jQuery, you can use the attr() method to get the class value of an element, the syntax format is "$(selector).attr('class')". The attr() method can return the attributes and values ​​of the selected element, and the class itself is an attribute of the element, so it can be obtained using the attr method.

How to get the value of class with jQuery

The operating environment of this tutorial: windows7 system, jquery1.10.0 version. This method is suitable for all brands of computers.

Recommended tutorial: jquery video tutorial

jQuery gets the value of the class

jQuery gets the value of the HTML attribute. An operation to do, since the class itself is also an attribute of the element, the value of the class can be obtained through the attr method.

The code is as follows:

$('#demo').attr('class');

attr() method can return the attributes and values ​​of the selected element, and return the value of the first matching element.

Syntax: $(selector).attr(attribute)

Complement: javascript method

You can also use the element object of javascript to obtain it. The code is as follows:

$('#demo')[0].className;

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of How to get the value of class with jQuery. 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
Previous article:what is hoc in reactNext article:what is hoc in react