Home  >  Article  >  Web Front-end  >  Use cssText to batch modify styles_javascript skills

Use cssText to batch modify styles_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:47:31861browse
Copy code The code is as follows:

var element= document.getElementById("id");
element.style.width="20px";
element.style.height="20px";
element.style.border="solid 1px red";

Style Yiduo , there is a lot of code; and overwriting the style of an object through JS is a typical process of destroying the original style and rebuilding it. This destruction and rebuilding will increase the browser's overhead.
There is a cssText method in js:
The syntax is: obj.style.cssText="style";
We can modify the above code to: Thanks to sliuqin for his correction in the message
element .style.cssText("width:20px;height:20px;border:solid 1px red;")
The following is correct
Copy code The code is as follows:

element.style.cssText="width:20px;height:20px;border:solid 1px red;";

In this way, page reflow can be avoided as much as possible and page performance can be improved.
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