Home > Web Front-end > JS Tutorial > jquery gets the value of class attribute

jquery gets the value of class attribute

藏色散人
Release: 2023-01-03 09:26:41
Original
12101 people have browsed it

The jquery method to get the value of the class attribute: first create a front-end sample file; then pass "$('#demo').attr('class');" or "$('#demo') [0].className;" method to get the value.

jquery gets the value of class attribute

The operating environment of this article: Windows 7 system, jquery version 1.2.6, Dell G3 computer.

jQuery obtaining the value of the class attribute is an operation that is often done. 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

Of course, you can also use the element object of javascript to obtain it. The code is as follows:

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

Of course, there are many ways to pass elements, these two methods That's enough, jquery also uses the attr method to set class

Extension:

attr() method sets or returns the attributes and values ​​of the selected element.

When this method is used to return an attribute value, the value of the first matching element is returned.

When this method is used to set attribute values, one or more attribute/value pairs are set for the matching element.

Syntax

Return the value of the attribute:

$(selector).attr(attribute)
Copy after login

Set the attribute and value:

$(selector).attr(attribute,value)
Copy after login

Use the function to set the attribute and value:

$(selector).attr(attribute,function(index,currentvalue))
Copy after login

Set multiple attributes and values:

$(selector).attr({attribute:value, attribute:value,...})
Copy after login

jquery gets the value of class attribute

[Recommended: jquery video tutorial]

The above is the detailed content of jquery gets the value of class attribute. 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