我会选择第一种方式——完全指定您使用的元素的样式。但这比我想象的要复杂一些。
首先,您必须完全指定容器元素。然后,对于其后代,您必须说明它们也应该使用默认值或从其父级继承(直到容器)。最后,您必须指定每个其他元素的外观,以便它们不都是普通的跨度。
相关的API是DOM Level 2 Style中的getComputedStyle
和CSSStyleSheet
接口。您可以使用那里的所有值,除了width
和height
,默认情况下应为auto
。您还需要下载一个默认样式表,例如Webkit用户代理样式表。然后,您可以调用以下函数创建完整的样式表,然后将其注入文档中。
请注意,在将样式表插入目标文档时,您必须使容器选择器尽可能具体,因为网页可能会给出比您的规则更高
specificity 的规则。例如,在
<html id=a><head id=b><style>#a #b * {weird overrides}</style></head>
中,
#a #b *
的特异性高于
#yourId div
,但我想这是不常见的。
注意:由于某种原因,Chrome 在加载 CSS 时给我报错 "无法加载资源",除非它已经在当前文档的
<link>
中了。因此,您应该在调用此函数的页面中包含 html.css。
var inheritedProperties = [
'azimuth', 'border-collapse', 'border-spacing', 'caption-side',
'color', 'cursor', 'direction', 'elevation', 'empty-cells',
'font-family', 'font-size', 'font-style', 'font-variant',
'font-weight', 'font', 'letter-spacing', 'line-height',
'list-style-image', 'list-style-position', 'list-style-type',
'list-style', 'orphans', 'pitch-range', 'pitch', 'quotes',
'richness', 'speak-header', 'speak-numeral', 'speak-punctuation',
'speak', 'speech-rate', 'stress', 'text-align', 'text-indent',
'text-transform', 'visibility', 'voice-family', 'volume',
'white-space', 'widows', 'word-spacing'];
inheritedProperties.push(
'hanging-punctuation', 'line-break', 'punctuation-trim',
'text-align-last', 'text-autospace', 'text-decoration-skip',
'text-emphasis', 'text-emphasis-color', 'text-emphasis-position',
'text-emphasis-style', 'text-justify', 'text-outline',
'text-shadow', 'text-underline-position', 'text-wrap',
'white-space-collapsing', 'word-break', 'word-wrap');
var completeStylesheet = function(containerSelector,
defaultStylesheetLocation) {
var rules = [];
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
try {
var span = iframe.contentDocument.createElement('span');
iframe.contentDocument.body.appendChild(span);
var basicStyle = iframe.contentDocument.defaultView.getComputedStyle(span);
var allPropertyValues = {};
Array.prototype.forEach.call(basicStyle, function(property) {
allPropertyValues[property] = basicStyle[property];
});
allPropertyValues['width'] = allPropertyValues['height'] = 'auto';
var declarations = [];
for (var property in allPropertyValues) {
var declaration = property + ': ' + allPropertyValues[property] + ';';
declarations.push(declaration);
}
rules.push({selectorText: containerSelector + ', ' +
containerSelector + ' *',
cssText: declarations.join(' ')});
rules.push({selectorText: containerSelector + ' *',
cssText: inheritedProperties.map(
function(property) {
return property + ': inherit;'
}).join(' ')});
if (defaultStylesheetLocation) {
var link = iframe.contentDocument.createElement('link');
link.rel = 'stylesheet';
link.href = defaultStylesheetLocation;
iframe.contentDocument.head.appendChild(link);
var sheet = link.sheet;
Array.prototype.forEach.call(
sheet.cssRules,
function(cssRule) {
rules.push({
selectorText: containerSelector + ' ' + cssRule.selectorText,
cssText: cssRule.style.cssText});
});
}
return rules;
} finally {
document.body.removeChild(iframe);
}
};
all: initial;
。 - Muhammad Umer