是否有现有的插件/应用程序/程序/脚本或其他任何东西,可以分析和计算文件中的CSS选择器数量?我想检查我的CSS文件在IE中无法工作的原因是因为我的选择器数量超过了4095(我很确定不是这个原因)
谢谢!
p.s. 如果有一个haml/sass/compass解决方案,则会额外加分。
是否有现有的插件/应用程序/程序/脚本或其他任何东西,可以分析和计算文件中的CSS选择器数量?我想检查我的CSS文件在IE中无法工作的原因是因为我的选择器数量超过了4095(我很确定不是这个原因)
谢谢!
p.s. 如果有一个haml/sass/compass解决方案,则会额外加分。
var
styleSheets = document.styleSheets,
totalStyleSheets = styleSheets.length;
for (var j = 0; j < totalStyleSheets; j++){
var
styleSheet = styleSheets[j],
rules = styleSheet.cssRules,
totalRulesInStylesheet = rules.length,
totalSelectorsInStylesheet = 0;
for (var i = 0; i < totalRulesInStylesheet; i++) {
if (rules[i].selectorText){
totalSelectorsInStylesheet += rules[i].selectorText.split(',').length;
}
}
console.log("Stylesheet: "+styleSheet.href);
console.log("Total rules: "+totalRulesInStylesheet);
console.log("Total selectors: "+totalSelectorsInStylesheet);
}
Error: The operation is insecure. "rules = styleSheet.cssRules,"
م€‚وœ‰ن»»ن½•وƒ³و³•هگ—ï¼ںن½؟用çڑ„وک¯Firefox 20.0.1ن¸ٹçڑ„FB 1.11.2م€‚ - RaphaelDDLh1,h2,h3 { margin: 0; }
。 - Philip Walton这将使用内联 CSS...
var selectors = 0;
$('style').each(function() {
var styles = $(this).html();
// Strip comments
styles = styles.replace(/\/\*.+?\*\//sg, '');
var matches = styles.match(/\{[\s.]*\}/g);
selectors += matches.length;
});
在jetli13的回答基础上,十年后没有IE选择器限制,但是未来会有styled-components和超级CSS in JS生成器,我添加了以下内容:
style
标签或external
样式表的样式添加您可以在SPA应用程序中路由更改后运行它,以查看添加了多少规则。
var
styleSheets = document.styleSheets,
totalStyleSheets = styleSheets.length,
overAllDocumentRules =0;
try {
for (var j = 0; j < totalStyleSheets; j++){
try{
var
styleSheet = styleSheets[j],
rules = styleSheet.cssRules,
totalRulesInStylesheet = rules.length,
totalSelectorsInStylesheet = 0;
for (var i = 0; i < totalRulesInStylesheet; i++) {
if (rules[i].selectorText){
totalSelectorsInStylesheet += rules[i].selectorText.split(',').length;
}
}
}
catch(err){
console.warn("skipping>", styleSheet.href, err);
console.warn(styleSheet)
}
if(totalRulesInStylesheet>1000) console.error("This stylesheet has over 1000 rules")
console.log("Stylesheet: "+styleSheet.href);
console.log(styleSheet)
console.info("Total rules: "+totalRulesInStylesheet);
overAllDocumentRules += totalRulesInStylesheet;
console.warn("Total Document Rules > "+ overAllDocumentRules)
}
}
catch(err){
console.warn("Error",err)
}
如果您需要在Safari Web Inspector中使用第三方样式,请执行以下操作: