[jQuery] Creating CSS Classes via JavaScript
I see that a lot of the time CSS styling is defined using
$("foo").css(). I think it is better to use addClass instead (although
it is less flexible). I have built a function that enables you to
create your own classes via JavaScript, and then you could use them
instead. However it does not work in Opera (due to it not supporting
document.styleSheets, although I haven't tested Opera 9), but I am
interested to know if it works in browsers other than Firefox or IE
(like Safari or Konqueror).
There is a way to do it with Opera, but it isn't pretty and will end
up with a new <style> element created each time and previous
definitions would not be overwritten (although maybe an id (like
'style{selectorText}') could be applied to it to work around that):
// create a new style sheet
var styleSheetElement = document.createElement("style");
styleSheetElement.type = "text/css";
// add to <head>
document.getElementsByTagName("head")[0].appendChild(styleSheetElement);
styleSheetElement.innerText = "pre{ font-weight: bold }";
It can be found at
http://webdevel.blogspot.com/2006/06/create-css-class-javascript.html
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/