Skip to content

Instantly share code, notes, and snippets.

@L1fescape
Created December 30, 2012 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L1fescape/4414186 to your computer and use it in GitHub Desktop.
Save L1fescape/4414186 to your computer and use it in GitHub Desktop.
Copying CSS
$.fn.copyCSS = function () {
var sheets = document.styleSheets,
o = {},
element = $(this[0]);
console.log(element)
for ( var i in sheets ) {
var rules = sheets[i].rules || sheets[i].cssRules;
for ( var r in rules ) {
if ( element.is(rules[r].selectorText) ) {
o = $.extend(o, css2json(rules[r].style), css2json(element.attr('style')));
}
}
}
return o;
}
function css2json(css){
var s = {};
if ( !css ) return s;
if ( css instanceof CSSStyleDeclaration ) {
for ( var i in css ) {
if( (css[i]).toLowerCase ) {
s[(css[i]).toLowerCase()] = (css[css[i]]);
}
}
}
else if( typeof css == "string" ) {
css = css.split("; ");
for ( var i in css ) {
var l = css[i].split(": ");
s[l[0].toLowerCase()] = (l[1]);
};
}
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment