在 webkit 中,你可以使用getMatchedCSSRules
实现你想要的。它按照浏览器应用的继承顺序返回一个 CSS 规则集,并且它是 webkit 检查器使用的 https://bugs.webkit.org/show_bug.cgi?id=79653前一段时间。对于像 Firefox 这样基于 Gecko 的浏览器,似乎有一个可用的 polyfillhere https://gist.github.com/ydaniv/3033012虽然我还没有测试过。
一个基本的、可行的解决方案
以下代码也可以作为小提琴使用 http://jsfiddle.net/UXJHt/
Because getMatchedCSSRules
仅适用于内联样式表 http://code.google.com/p/chromium/issues/detail?id=69626,您首先必须内联链接的样式表:
function inlineStyles() {
var stylesheets = document.getElementsByTagName('link'),
head = document.getElementsByTagName('head')[0];
for (var i = 0; i < stylesheets.length; i++) {
if (stylesheets[i].getAttribute('rel') == 'stylesheet') {
(function (stylesheet) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", stylesheet.getAttribute('href'), true);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var inlineStyle = document.createElement('style');
inlineStyle.setAttribute('type', 'text/css');
inlineStyle.innerText = xmlhttp.responseText;
head.replaceChild(inlineStyle, stylesheet);
}
};
xmlhttp.send();
})(stylesheets[i]);
} else {
continue;
}
};
}
然后,大块:这是第一枪,随意改进。它可以处理inherit
规则,最多 1 个!important
定义,但仅此而已。对于非常复杂的设置,必须改进:
function getStyle(s, id) {
var element = typeof id === 'string' ? document.getElementById(id) : id,
css = window.getMatchedCSSRules(element),
style = window.getComputedStyle(element),
value = style[s],
styles = [],
rules = [],
inherited, currentRule;
// if there's a computed style, start calculation
if (value) {
// add matched rules if there are any
if (css) {
for (var i = 0; i < css.length; i++) {
styles.push(css[i]);
}
}
// add the element style attribute as a matched rule
styles.push({
style: element.style,
cssText: 'element.style {' + element.getAttribute('style') + ' }'
});
for (var i = styles.length - 1; i >= 0; i--) {
var def = styles[i],
rule = {
index: rules.length,
style: s,
value: styles[i].style[s],
cssText: def.cssText
};
if (rule.value == 'inherit' && !currentRule) {
if (inherited = getInherited(element, s, value)) {
rule.inheritedFrom = inherited;
currentRule = rule;
inherited = undefined;
} else {
rules.push(rule);
}
} else if (rule.value == 'inherit' && currentRule && isImportant(s, value, def.cssText)) {
if (inherited = getInherited(element, s, def)) {
rule.inheritedFrom = inherited;
rules.splice(currentRule.index, 0, currentRule);
currentRule = rule;
inherited = undefined;
} else {
rules.push(rule);
}
} else if (rule.value == value && !currentRule) {
currentRule = rule;
} else if (rule.value == value && currentRule && isImportant(s, value, def.cssText)) {
rules.splice(currentRule.index, 0, currentRule);
currentRule = rule;
} else if (rule.value.length) {
rules.push(rule)
}
}
return {
current: currentRule,
overwritten: rules
};
} else {
return false;
}
}
如果发生继承,我们会沿着 DOM 节点查找使用此辅助函数定义 CSS 规则的元素并获取其样式:
function getInherited(element, s, value) {
while (element.parentNode && window.getComputedStyle(element.parentNode)[s] == value) {
element = element.parentNode;
}
if (element) {
return getStyle(s, element).current;
} else {
return null;
}
}
我们使用此辅助函数确定 CSS 规则是否被标记为重要:
function isImportant(s, style, text) {
return new RegExp(s.replace(/([A-Z])/g, '-$1').toLowerCase() + ':\\s+' + style + '\\s+!important').test(text)
}
看看小提琴,看看它是否工作 http://jsfiddle.net/UXJHt/