我想返回一个包含 CSS 规则所有内容的字符串,就像您在内联样式中看到的格式一样。我希望能够在不知道特定规则中包含什么内容的情况下做到这一点,所以我不能仅仅通过样式名称(如 .style.width
等)将它们提取出来
CSS:
.test {
width:80px;
height:50px;
background-color:#808080;
}
到目前为止的代码:
function getStyle(className) {
var classes = document.styleSheets[0].rules || document.styleSheets[0].cssRules
for(var x=0;x<classes.length;x++) {
if(classes[x].selectorText==className) {
//this is where I can collect the style information, but how?
}
}
}
getStyle('.test')