Home > Article > Web Front-end > How to modify css in javascript
In web development, we often need to modify CSS styles through JavaScript and dynamically change element styles in web pages to achieve a better user interaction experience. This article will introduce how JavaScript modifies CSS, including the following aspects:
1. Modify element style
To modify the style of an element, we can do it through the style attribute of JavaScript. The style attribute represents the inline style of the element, and the style of the element can be directly modified through this attribute. For example, we can set the background color of a div element to red through the following code:
var divElement = document.getElementById("example"); divElement.style.backgroundColor = "red";
Here, we use the getElementById function to get the div element with the id example, and then use the style attribute to set it background color.
In addition to backgroundColor, we can also set many other styles of elements through the style attribute, such as color, font, padding, etc. The specific syntax format is:
element.style.property = value;
Among them, property is the style Attribute name, value is the attribute value, for example:
divElement.style.color = "blue"; divElement.style.fontSize = "20px"; divElement.style.padding = "10px";
2. Dynamically add a style sheet
Sometimes, we need to dynamically add a new style sheet to the web page to achieve a more flexible style control. At this time, we can use JavaScript's createElement and appendChild functions to dynamically add style sheets.
The specific steps are as follows:
var linkElement = document.createElement("link");
linkElement.rel = "stylesheet"; linkElement.type = "text/css"; linkElement.href = "style.css";
var headElement = document.getElementsByTagName("head")[0]; headElement.appendChild(linkElement);
In this way, the style sheet is successfully added to the web page. It should be noted that before adding a style sheet, we need to ensure that the style sheet file has been loaded. Otherwise, the style may not take effect.
3. Get the calculated style
Sometimes, we need to get the calculated style of an element, that is, the final style after all style rules are applied to the element. For example, if we need to get the calculated background color of a certain div element, we can use JavaScript's getComputedStyle function to achieve this.
The specific operation steps are as follows:
var divElement = document.getElementById("example");
var computedStyle = window.getComputedStyle(divElement);
var backgroundColor = computedStyle.backgroundColor;
It should be noted here that the getComputedStyle function returns a CSSStyleDeclaration object, which contains all calculated styles of the element. If you want to get the value of a certain attribute, you need to use the corresponding attribute of the object. For example, computedStyle.backgroundColor represents the calculated background color of the element.
4. Modify the class name
Sometimes, we need to change the style of the element by modifying the class name. For example, we need to switch the background color of a div element by clicking a button, which can be achieved by modifying the class attribute of the element.
The specific steps are as follows:
var buttonElement = document.createElement("button"); buttonElement.textContent = "切换背景色";
buttonElement.addEventListener("click", function() { var divElement = document.getElementById("example"); divElement.classList.toggle("highlight"); });
Here, we added a click event handling function, which uses the classList attribute to modify the class attribute of the element. We use the toggle function to switch whether the element contains the highlight class name. If the element does not originally contain the highlight class name, add the class name; otherwise, remove the class name.
document.body.appendChild(buttonElement);
In this way, you can dynamically switch the background color of the div element by clicking the button.
Summary:
The style control function of JavaScript provides us with a rich web page style control method. By mastering the skills introduced above, we can achieve a more flexible and interactive web page style. Effect. At the same time, style control is also one of the essential skills for Web front-end development. I hope this article can be helpful to readers.
The above is the detailed content of How to modify css in javascript. For more information, please follow other related articles on the PHP Chinese website!