Home  >  Article  >  Web Front-end  >  How to change the class of div with jquery

How to change the class of div with jquery

WBOY
WBOYOriginal
2022-04-20 17:21:232563browse

In jquery, you can use the attr() method to change the class of a div. This method can set or return the attributes and values ​​of the element. Just set the first parameter in the method to "class". Just set the two parameters to the changed values, and the syntax is "div object.attr('class', new attribute value)".

How to change the class of div with jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to change the class of div in jquery

In jquery, you can use the attr() method to replace the class attribute of the tag.

The example is as follows:

Create a new html file, named test.html, to explain how jquery modifies the class attribute of div, and uses the div tag to create a line of text for testing. .

Set the id attribute of the div tag to kp. Use the ready method to execute the function function when the page is loaded.

In the function, obtain the div object through the id, and use the attr() method to modify the class attribute of the div to newcc.

How to change the class of div with jquery

Open the test.html file in the browser to view the results.

How to change the class of div with jquery

Summary:

1. Use the div tag to create a line of text and set the id attribute of the div tag to kp.

2. In the js tag, use the ready method to execute the function function when the page is loaded.

3. In the function, obtain the div object through the id, and use the attr() method to change the class attribute of the div to newcc.

4. Open the test.html file in the browser and view the results.

Recommended related video tutorials: jQuery video tutorial

The above is the detailed content of How to change the class of div 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