Brilliant minds think alike :) The RegExp allows for stylesheets that use a comma, like
.classname , .otherclass {...} which my simple == wouldn't catch. On the other hand, yours would catch .classname childelement {...} which wouldn't be right. The quick and dirty answer is to make sure your stylesheets don't do that. The right answer would be to change the condition to if ('/[^ ,]'.selector.'[$ ,]/'.test(this.selectorText)) but I haven't tested that. Adding the checks for the appropriate media types is left as an exercise for the reader. thumblewend wrote: > > On 16/03/2007, at 10:01 AM, Danny Wachsstock wrote: >> function styleString(selector){ >> var style = ""; >> $.each (document.styleSheets, function(){ >> $.each (this.cssRules || this.rules, function() { >> if (this.selectorText == selector) style += >> this.style.cssText + ';'; >> }); >> }); >> return style; >> } > > Haha, that's funny. I was just about to post my rewrite and the code > is very similar to yours except yours is better due to the use of || > instead of my ternary, plus I used a regexp as per the original which > from your code looks like it's not needed. Mine's untested too. Here > it is for giggles: > > $.getClassContent = function(classname) { > var cssText = ''; > $.each(document.styleSheets,function() { > var cssRules = (typeof this.cssRules == 'array') ? > this.cssRules : > this.rules; > $.each(cssRules,function() { > if ('/\.'+classname+'/'.test(this.selectorText)){ > cssText = this.style.cssText; > } > }); > }); > return cssText; > }; > > Joel. > > _______________________________________________ > jQuery mailing list > discuss@jquery.com > http://jquery.com/discuss/ > > -- View this message in context: http://www.nabble.com/How-to-convert-%22class%22-to-%22style%3D%27attrs-...%27%22-tf3409582.html#a9506865 Sent from the JQuery mailing list archive at Nabble.com. _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/