Javascript 如何使用jQuery(或类似工具)提取所有CSS
给定一个网页,该网页已通过以下方式加载(可能有多个)CSS文件和内联样式:Javascript 如何使用jQuery(或类似工具)提取所有CSS,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" /> &
<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 -->
/*一些css规则*/
/*其他一些css规则*/
如何编写一个函数(可能使用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;
}
函数getStyles(){
如果(!document.styleSheets)返回false;//如果浏览器不工作,则返回false
var规则=新数组();
for(var i=0;i调用此函数时,它将返回所有规则的数组。在Firefox中测试,即。我不确定您想用它做什么,但这应该可以让您开始:
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;
}
函数getStyles(){
如果(!document.styleSheets)返回false;//如果浏览器不工作,则返回false
var规则=新数组();
for(var i=0;i调用此函数时,它将返回所有规则的数组。在Firefox中测试,即。将其提取到什么?将.css文件的内容转换成一个字符串?我想他指的是firebug所做的事情,如编码样式或计算样式?将其转换成什么?将.css文件的内容转换成一个字符串?我想他指的是firebug所做的事情,如编码样式或计算样式?