Get the App
SLTechnology News&Howtos  ›  Development  › 

How to add classes to jquery

Shulou Source: shulou.com Published: 2022-05-31 16:29:05 10月03日 Update

This article mainly explains "jquery how to add classes", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "jquery how to add classes"!

Two methods: 1, with attr (), just add a value to the class attribute, syntax "element object .attr (" class "," class name ");". 2, with addClass (), add one or more classes to the syntax "element object .addClass (" class name ")". If you add more classes, separate the class names with spaces.

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

Two methods of adding classes to jquery

1. Use attr ()

Attr () can set the attribute value of the selected element and add a class when the attribute is "class".

This method is used to add a class when there is no class before.

Example: add a class to the first p element

$(document) .ready (function () {$("button") .click (function () {$("p:first"). Attr ("class", "intro");}) .intro {font-size: 120%; color: red;} This is a heading

This is a paragraph.

This is another paragraph.

Add a class to the first p element

2. Use addClass ()

The addClass () method adds one or more classes to the selected element.

This method does not remove the existing class attribute, but simply adds one or more class attributes. To add more than one class, separate the class names with spaces.

Example: add a class to the second p element

$(document) .ready (function () {$("button") .click (function () {$("p:nth-child (3)") .addClass ("intro");}) .intro {font-size: 120%; color: red;} a subtitle

The first paragraph

The second paragraph

The third paragraph

Add a class to the second p element

Thank you for your reading, the above is the content of "how to increase the class of jquery". After the study of this article, I believe you have a deeper understanding of how to increase the class of jquery, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Elements multiple attributes methods class names paragraphs learning content objects examples spaces syntax three just ideas situations tutorials articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi Docker Microsoft vpn