Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Last active December 19, 2015 17:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gaubee/5994208 to your computer and use it in GitHub Desktop.
Save Gaubee/5994208 to your computer and use it in GitHub Desktop.
write all element's style in the prototye of style. 将元素所有的样式写到style中,方便单文件复制。 mhtml的保存并不完整。
(function (G) {
function simpleDOM(tagName){
var dom = document.createElement(tagName);
document.body.appendChild(dom);
return dom;
}
function getDifStyle(el){
var baseDOM = simpleDOM(el.tagName);
var elStyle = getComputedStyle(el);
var baseDOMStyle = getComputedStyle(baseDOM);
var resultStyle = {};
for(var i in elStyle){
if (parseInt(i)!=i) {
resultStyle[i] = elStyle[i];
}
}
for(var i in resultStyle){
if (i.indexOf("webkit")!==-1||resultStyle[i] === baseDOMStyle[i]) {
delete resultStyle[i];
}
}
delete resultStyle["cssText"];
document.body.removeChild(baseDOM);
return resultStyle;
}
G.getDifStyle = getDifStyle;
G.writeAllStyle = function(){
var allDOM = document.all;
var catchStyle;
var style;
for(var i = 0,itemDOM;itemDOM = allDOM[i];i+=1){
catchStyle = getDifStyle(itemDOM);
style = itemDOM.style;
for(var j in catchStyle){
style[j] = catchStyle[j]
}
console.log("DOM in "+i);
}
console.log("style write ok!");
}
}(window))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment