/ Published in: JavaScript
URL: http://pjotor.com
I needed something that could extract style from a css-class, even if no DOM-object had the class; thus this little plugin.
Usage: $.cssStyle(".className") Returns a Object with key/value pairs corresponding to the style of the CSS-class "className"
Expand |
Embed | Plain Text
// Style sheet getter // Returns style as object usage: // var classNameColor = $.cssStyle(".className").color; ;(function ($) { $.cssStyle = function (selector) { var sels = selector.match(/(^|\#|\.|\s)[\w|-]+/g), ret = {}, style, sel, i; $.each( document.styleSheets, function () { var rules = this.cssRules || this.rules; $.each(rules, function (n, v) { for (i in sels) { sel = new RegExp("(?:^|\\s)(" + sels[i].replace(/^\s+/, "") + ")(?=,|$)"); if (v.selectorText && sel.test(v.selectorText)) { $.extend(ret, $.map.str(v.style.cssText)); } } }); }); return ret; } })(jQuery); // a little string mapper (string to object) ;(function ($) { $.map.str = function (s, objDev, keyDev) { objDev = objDev || ";", keyDev = keyDev || ":"; if (s.indexOf(keyDev) === -1) return s; var d = s.split(objDev); var r = {}; var key, val; for(par in d) { val = d[par].split(keyDev); key = $.trim(val.splice(0, 1).join()); val = $.trim(val.join(keyDev)); if (val.length > 1) { r[$.camelCase(key)] = val; } } return r; } })(jQuery); // CamelCase object keys (ripped from jQuery since it seems to come and go in core) ;(function ($) { if ( !$.isFunction($.camelCase) ) { $.camelCase = function (string) { return string.replace(/-([a-z])/ig, function (all, letter) { return letter.toUpperCase(); }); } } })(jQuery);
You need to login to post a comment.
