【问题标题】:How to use jQuery (or similar) to extract all of the CSS如何使用 jQuery(或类似的)提取所有 CSS
【发布时间】:2009-03-13 02:09:07
【问题描述】:

给定一个已经加载(可能有多个)CSS 文件和内联样式的网页,通过使用:

<link rel="stylesheet" type="text/css" href="some_file.css" />
<style type="text/css"> /* some css rules */  </style>
<link rel="stylesheet" type="text/css" href="some_other_file.css" />
<style type="text/css"> /* some other css rules */  </style>      
<!-- etc -->

如何编写一个函数(可能使用 jQuery)来提取所有有效的网络 CSS 规则?

【问题讨论】:

  • 解压成什么?将.css文件的内容转成字符串?
  • 我认为他的意思就像萤火虫所做的那样
  • 级联样式还是计算样式?

标签: javascript jquery css


【解决方案1】:

我不确定你想用这个做什么,但这应该让你开始:

function getStyles() {
    if(!document.styleSheets) return false; // return false if browser sucks
    var rules = new Array();
    for (var i=0; i < document.styleSheets.length; i++) {
        var x = 0;
        styleSheet = document.styleSheets[i];
        if(styleSheet.cssText) { // if this is IE, get the rules directly
            rules.push(styleSheet.cssText);
        } else {
            // otherwise get them individually
            do {
                cssRule = styleSheet.cssRules[x];
                if(cssRule) rules.push(cssRule.cssText);
                x++;
            } while (cssRule);
        }
    }
    return rules;
}

当你调用它时,它会返回一个包含所有规则的数组。在 Firefox、IE 中测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 2013-05-10
    • 2019-05-13
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多