[jQuery] Style sheet modification snippet

[jQuery] Style sheet modification snippet

Today I found the need to actually create a stylesheet and add rules
to it instead of using the style property. The style property puts any
of the styles inline and this really hurt my print styles. So, for a
few styles in my site I used this snippet to actually add a style
sheet and rules. It is a first draft and lacks a lot but thought I
would share anyways.
jQuery.style = function(selector, declaration) {
if (!jQuery.style.sheet) jQuery.style.createSheet();
var s = jQuery.style.sheet;
var n = jQuery.style.node;
if ($.browser.safari) {
n.appendChild( document.createTextNode(selector+" { "+declaration+" }") );
} else if (s && s.insertRule) {
s.insertRule(selector+" { "+declaration+" }", s.cssRules.length);
} else if (s && s.addRule) {
s.addRule(selector, declaration, s.rules.length);
}
};
jQuery.extend(jQuery.style, {
sheet: null,
node: null,
createSheet: function() {
var style = document.createElement('style');
style.setAttribute('type', 'text/css');
style.setAttribute('media', 'screen');
jQuery.style.node =
document.getElementsByTagName('head')[0].appendChild(style);
jQuery.style.sheet = document.styleSheets[document.styleSheets.length-1];
}
});
--
Brandon Aaron
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/