Home >Web Front-end >JS Tutorial >How Can I Retrieve the Contents of CSS Rules in JavaScript?
Getting the Contents of CSS Rules
Knowing how to access the values of CSS rules can be valuable for dynamic styling or manipulating the presentation of your elements.
To return a string containing all the contents of a CSS rule in inline style format, you can use the following approach:
function getStyle(className) {</p> <pre class="brush:php;toolbar:false">var cssText = ""; var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules; for (var x = 0; x < classes.length; x++) { if (classes[x].selectorText == className) { cssText += classes[x].cssText || classes[x].style.cssText; } } return cssText;
}
alert(getStyle('.test'));
In this code:
The above is the detailed content of How Can I Retrieve the Contents of CSS Rules in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!