相关疑难解决方法(0)

你如何用JavaScript阅读CSS规则值?

我想返回一个包含CSS规则所有内容的字符串,就像您在内联样式中看到的格式一样.我希望能够在不知道特定规则中包含的内容的情况下执行此操作,因此我不能仅通过样式名称将其拉出(如.style.width等)

CSS:

.test {
    width:80px;
    height:50px;
    background-color:#808080;
}
Run Code Online (Sandbox Code Playgroud)

到目前为止的代码:

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')
Run Code Online (Sandbox Code Playgroud)

html javascript css

106
推荐指数
5
解决办法
11万
查看次数

标签 统计

css ×1

html ×1

javascript ×1