javascript - 如何使用 jQuery(或类似的)来提取所有的 CSS

标签 javascript jquery css

给定一个已加载(可能有多个)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 规则?

最佳答案

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

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 中测试。

关于javascript - 如何使用 jQuery(或类似的)来提取所有的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/641221/

相关文章:

javascript - 用于构建并排 d3.js 图表的嵌套 JSON 结构

javascript - Base64 Alfresco 上的 GET 请求

jQuery 如果 li 里面有一个 ul 子元素,则使它成为第一个显示的列表项

jquery - 如何根据href属性找到页面中的链接?

javascript - jquery dataTables.js 及其对应的 compatibile jquery.js

javascript - response.setHeader 和 response.writeHead 的区别?

javascript - 我不希望 datetimepicker 在点击时设置默认值

css - 可以 flex : 1 and minus px?

javascript - 分别检测哪个 li 和 hover

html - 如何使用 css 在 html 中给表格加边框