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

How to get the value of class with jQuery

青灯夜游
Release: 2020-12-18 17:43:51
Original
9685 people have browsed it

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

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

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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template