Home >Web Front-end >JS Tutorial >How to set the css of div in js

How to set the css of div in js

藏色散人
藏色散人Original
2021-04-29 10:26:294368browse

JS method to set the css of div: first introduce the Jquery plug-in; then change the style of html by using jquery's css method, or use jquery's addClass method to change or add a css style to html.

How to set the css of div in js

The operating environment of this article: windows7 system, jquery version 1.8.3, Dell G3 computer.

In order to be more convenient and easier to use, we introduce the Jquery plug-in and use Jquery to operate.

How to set the css of div in js

Our html is very simple, just a div with a paragraph of text inside.

How to set the css of div in js

Open the page, now the div does not use css style,

How to set the css of div in js

To change the style of html, we can use it directly jquery css method:

$("#div_d").css("background-color","#0e90d2");

How to set the css of div in js

Refresh the page and you can see that the background color of the div has now turned blue.

How to set the css of div in js

When multiple css style values ​​need to be modified, directly changing the css is troublesome and requires multiple calls. At this time, our more reasonable method is to change or add a css style to html.

Use jquery's addClass method: $("#div_d").addClass("div_class");

How to set the css of div in js

##Our css style div_class is defined as shown in the figure , multiple style values ​​are defined.

How to set the css of div in js

Look at the effect of the current page again, the style has been added successfully. [Recommended learning:

javascript advanced tutorial]

How to set the css of div in js

The above is the detailed content of How to set the css of div in js. 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