1. Get the element style:
You can obtain the in-line style of the element through the style attribute of the element. The style attribute is an object that includes a series of style attributes. For example: color, backgourdColor.
The above mentioned element style is not recommended to obtain element styles through the style attribute.
The following piece of code can get the style of the element when it runs, that is, the global style. This way you can dynamically get the style of an element, such as the element size.
// node: The element node that will be obtained whose computed style is // attr: style attribute name function getCurrentStyle(node, attr) { var style = null; //dom standard method if(window.getComputedStyle) { style = window.getComputedStyle(node, null); } else{ style = node.currentStyle; //ie method} return style[attr];}2. Modify the element style
Directly through the element's style attribute, for example: p.style.backgroundColor = "red"
Note: When obtaining or modifying element styles through the above method, there is a difference between the attribute name and the defined element attribute name. For example: define the background color through the background-color of css, then when obtaining or modifying this style attribute in js, the first letter after the '-' symbol needs to be converted to size.
The above example code for obtaining and modifying element styles of JS is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.