The methods are: 1. Through node.style.cssText="css expression 1; css expression 2; css expression 3"; 2. First set the specific class in the CSS style sheet Style, and then attach the style setting of the class to the node node through node.classname="class name".
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
Below I will introduce to you two ways to change CSS styles in native js:
1Through ## in the javascript code #node.style.cssText="css expression 1; css expression 2; css expression 3 " directly changes the CSS style.
2First set the style for a specific class such as the "active class" in the CSS style sheet (the active class here is assumed and does not exist for the time being), and then in the javascript code Through node.classname="active", the style setting of the active class in the CSS style sheet is attached to the node node.
The following is a detailed introduction, first is the html code:<style type="text/css"> div { float: left; padding: 20px; margin: 10px; border: 1px solid #000; background-color: #fff; color: #000; } .active { background-color:blue } </style> <body> <div class="root"> </div> </body>
<script type="text/javascript"> var root=document.getElementsByClassName("root")[0]; root.style.cssText="background-color: blue;color: #fff;"; </script>
<script type="text/javascript"> var root=document.getElementsByClassName("root")[0]; root.className="active";</script>
The above is the detailed content of How to modify css in native js. For more information, please follow other related articles on the PHP Chinese website!