How to modify css in js

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

How to modify css in js: 1. Use [obj.style.cssTest] to modify the embedded css; 2. Use [bj.className] to modify the class name of the style sheet; 3. Use the change external linked css file, thereby changing the css of the element.

How to modify css in js

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

How to modify css in js:

Method 1. Use obj.style.cssTest to modify embedded css

function changeStyle2() {
   var obj = document.getElementById("btnB");
   obj.style.cssText = " display:block;color:White;
 
}
Copy after login

Method 2. Use bj.className to modify the class name of the style sheet

Use code to modify the class name of the btB reference style, as shown in the following code:

function changeStyle3() {
  var obj = document.getElementById("btnB");
  //obj.className = "style2";
  obj.setAttribute("class", "style2");
}
Copy after login

Change the style by changing the class name of btB's css. There are two ways to change the style class name. 1. obj.className = "style2"; 2. obj.setAttribute("class", "style2"); have the same effect.

Using this method to modify css has a much better effect than the above.

Method 3: Change the css file of the external link to change the css of the element

Change the style of btB by changing the reference of the external css file, operation Very simple. The code is as follows:

First, you must quote the external css file, the code is as follows:

<link href="css1.css" rel="stylesheet" type="text/css" id="css"/>
 
function changeStyle4() {
   var obj = document.getElementById("css");
   obj.setAttribute("href","css2.css");
 }
Copy after login

Recommended related tutorials: CSS video tutorial

The above is the detailed content of How to modify css in js. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!