JS batch operation CSS properties detailed parsing

  • 2020-03-30 00:56:09
  • OfStack


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .day
        {
         background-color:White;
        }
        .night
        {
         background-color:Black
        }
    </style>
    <script language="javascript" type="text/javascript">
        function operStyle() {
            var divObj = document.getElementById("divContent");
            var btnObj = document.getElementById("btnCommit");
            if (divObj.className == "day") {
                divObj.className = "night";
                btnObj.value = " Turn on the light ";
            } else {
            divObj.className = "day";
            btnObj.value = " Turn off the lights ";
            }
        }
        //Batch modify div style properties, by a variety of styles
        //Method 1:
        function methodOne() {
            var divObj = document.getElementById("divTest");
            divObj.style.backgroundColor = "blue";
            divObj.style.border = "solid 1px red";
            divObj.style.width = "300px";
            divObj.style.height = "200px";
            divObj.style.backgroundPosition = "center";
        }
        //Method 2:
        function methodTwo() {
            var divObj = document.getElementById("divTest");
            divObj.style.cssText = "background-color:Blue; border:solid 1px red; width:300px; height:200px; background-position:center";
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="divContent" class="day">
       <font color="red"> I am a div O, e-i-e-i-o! </font>
    </div>
    <input type="button" value=" Turn off the lights " id="btnCommit" onclick="operStyle();" />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <hr />
    <h1> Modify the divTest Style, multi - attribute operation </h1>
    <div id="divTest" >
        <font color="red"> Modify the divTest The style of the </font>
    </div>
    <input type="button" value=" Modify the divTest The style of the " onclick="methodTwo()" />
    </form>
</body>
</html>

CSS styles written in js are usually written in the following two ways:

In general, we use js to set the style of the element object.
Copy the code
Var element = document. GetElementById (" id ");
Element. Style. 20 px "width =";
Element. Style. 20 px height = "";
Element. Style. 1 px red "border =" solid;

But the above method has a disadvantage, the style is more, the code is more; Moreover, the style of overwriting objects by JS is a typical process of destroying the original style and rebuilding, which will increase the browser's overhead.

Js has a cssText method:
The syntax is obj.style.cssText(" style ");
The above code can be changed to:
Copy the code
Element. Style. CssText = "width: 20 px; Height: 20 px; 1 px red border: solid;" ;

This method can avoid multiple reflows of the page and improve the performance of the page.


Related articles: