Usually I have sầu a CSS file which has the following rule:


How can I avoid creating such a static CSS tệp tin by adding the CSS information during runtime actions to lớn the toàn thân, or something similar? (only using jQuery)

I want to define it once but with jQuery and use it many times later; that"s why I bởi not want khổng lồ add it each time khổng lồ the specific DOM elements.

Bạn đang xem: Create css class in jquery

I know the simple features (css("attr1", "value");), but how can I create a complete reusable CSS rule?

You can create style element and insert it into DOM

$("").appendTo("head"); $("
").addClass("redbold").text("SOME NEW TEXT").appendTo("body");

tested on Opera10 FF3.5 iE8 iE6

The gist is self sufficient. You can create it locally, và it will work just fine. Direct link to the tải về link: – Daniel Ribeiro Nov 25 "10 at 19:43
No problem. Sometimes it can be jQuery minor version bug, or just an incorrect setup. The giảm giá with js apps is this: if they don't work, they just don't work at all. No forgiveness. – Daniel Ribeiro Nov 27 "10 at 0:37
Throws an 'Unexpected Hotline lớn method or property access' error for me in IE8. I had lớn follow this article to get it working. – Brandon Boone May 7 "12 at 13:24
There can be only 31 styles in page in IE9-… – IvanH Nov 16 "12 at 13:22
If it doesn't work. Try append style to toàn thân. i.e. $("").appendTo("body"); – inDream Mar 21 "13 at 11:33