Add string to CSS code with Javascript
I need to add a string before CSS elements with Javascript code. An example of the original code:
h1 {
background: red;
}
.class开发者_C百科 #id {
color: black;
}
I want Javascript to filter it like this:
.myclass h1 {
background: red;
}
.myclass .class #id {
color: black;
}
Let's say the CSS-code is a Javascript string called str.
The best way to do this is to have the CSS already written and ready, and use javascript to add the needed class to the desired HTML elements.
Using jQuery you would do something like ("#id").addClass("myclass")
Where myclass is the name of the class which is used in your css.
You can dynamically add css manually using ("#id").css("...css...")
精彩评论