How js changes css style

coldplay.xixi
Release: 2023-01-05 16:09:13
Original
4383 people have browsed it

How to change the css style in js: 1. Use the cssText method; 2. Use the [setProperty()] method; 3. Use the style attribute corresponding to the css attribute.

How js changes css style

The operating environment of this tutorial: windows7 system, css3 version, DELL G3 computer.

JS method to change css style:

The first one: use cssText

div.style.cssText='width:600px;height:250px;border:1px red solid;text-align: center;line-height: 250px;';
Copy after login

The second one : Use setProperty()

 div.style.setProperty('width','700px');
 div.style.setProperty('height','300px');
 div.style.setProperty('line-height','300px');
 div.style.setProperty('background','#f00');
 div.style.setProperty('color','#fff');
 div.style.setProperty('border','1px solid blue');
 div.style.setProperty('text-align','center');
Copy after login

Third method: Use the style attribute corresponding to the css attribute

 div.style.width = "800px";
 div.style.height = "250px"; 
 div.style.lineHeight = "250px"; 
 div.style.background = "#000";
 div.style.color = "#fff";
 div.style.border = "1px solid #111";
 div.style.textAlign = "center";
Copy after login

        
这是一个盒子
Copy after login

Recommended related tutorials:CSS Video Tutorial

The above is the detailed content of How js changes css style. 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