Using JS to dynamically set the CSS style, there are several common
1. Set the properties of the style directly some cases with this setting!important value is invalid
If the attribute has a '-' number, it is written in the form of a hump (such as textalign) if you want to preserve the-number, in the form of brackets element.style[' text-align '] = ' 100px ';
Element.style.height = ' 100px ';
2. Setting properties directly (only for certain properties, related styles are automatically recognized)
Element.setattribute (' height ', 100);
Element.setattribute (' height ', ' 100px ');
3. Set the properties of the style
Element.setattribute (' style ', ' height:100px!important ');
4. Use SetProperty If you want to set up a!important, it is recommended to set the third parameter in this way
Element.style.setProperty (' height ', ' 300px ', ' important ');
5. changing class-related methods such as JQ
Because JS gets no pseudo-elements of the CSS, you can change the style of the pseudo-element dynamically by changing the class of the pseudo-element's parent.
Element.classname = ' Blue '; element.classname + = ' Blue fb ';
6. Set Csstext
Element.style.cssText = ' height:100px!important '; element.style.cssText + = ' height:100px!important ';
7. Create a new CSS style file to introduce
function Addnewstyle (newstyle) { var styleelement = document.getElementById (' styles_js '); if (! styleelement) {styleelement = document.createelement (' style '); styleelement.type = ' text/css '; styleelement.id = ' Styles_js '; document.getElementsByTagName (' head ') [0].appendchild (styleelement);} Styleelement.appendchild (document.createTextNode (NewStyle)); } addnewstyle ('. box {height:100px!important;} ');
8. Using Addrule, Insertrule
in the original style operation document.stylesheets[0].addrule ('. Box ', ' height:100px '); Document.stylesheets[0].insertrule ('. box {height:100px} ', 0); //var styleel = document.createelement (' style '), styleSheet = styleel.sheet; Stylesheet.addrule ('. Box ', ' height:100px '); Stylesheet.insertrule ('. box {height:100px} ', 0
Using JS to dynamically set the CSS style, there are several common
1. Set the properties of the style directly some cases with this setting!important value is invalid
If the attribute has a '-' number, it is written in the form of a hump (such as textalign) if you want to preserve the-number, in the form of brackets element.style[' text-align '] = ' 100px ';
Element.style.height = ' 100px ';
2. Setting properties directly (only for certain properties, related styles are automatically recognized)
Element.setattribute (' height ', 100);
Element.setattribute (' height ', ' 100px ');
3. Set the properties of the style
Element.setattribute (' style ', ' height:100px!important ');
4. Use SetProperty If you want to set up a!important, it is recommended to set the third parameter in this way
Element.style.setProperty (' height ', ' 300px ', ' important ');
5. changing class-related methods such as JQ
Because JS gets no pseudo-elements of the CSS, you can change the style of the pseudo-element dynamically by changing the class of the pseudo-element's parent.
Element.classname = ' Blue '; element.classname + = ' Blue fb ';
6. Set Csstext
Element.style.cssText = ' height:100px!important '; element.style.cssText + = ' height:100px!important ';
7. Create a new CSS style file to introduce
function Addnewstyle (newstyle) { var styleelement = document.getElementById (' styles_js '); if (! styleelement) {styleelement = document.createelement (' style '); styleelement.type = ' text/css '; styleelement.id = ' Styles_js '; document.getElementsByTagName (' head ') [0].appendchild (styleelement);} Styleelement.appendchild (document.createTextNode (NewStyle)); } addnewstyle ('. box {height:100px!important;} ');
8. Using Addrule, Insertrule
in the original style operation document.stylesheets[0].addrule ('. Box ', ' height:100px '); Document.stylesheets[0].insertrule ('. box {height:100px} ', 0); //var styleel = document.createelement (' style '), styleSheet = styleel.sheet; Stylesheet.addrule ('. Box ', ' height:100px '); Stylesheet.insertrule ('. box {height:100px} ', 0
JS set CSS styles in several ways