Define Global CSS Classes Using JavaScript Or JQuery?
Is there a way to set the CSS of global classes using JavaScript or jQuery? That is, append .my_class { foo:bar } to the tag of the page?
Solution 1:
Pure javascript -
var style=document.createElement('style');
style.type='text/css';
if(style.styleSheet){
style.styleSheet.cssText='your css styles';
}else{
style.appendChild(document.createTextNode('your css styles'));
}
document.getElementsByTagName('head')[0].appendChild(style);
Solution 2:
Yes, you can do that in jQuery:
var styleTag = $('<style>.my_class { foo: bar; }</style>')
$('html > head').append(styleTag);
It works by simply appending <style>
tag at the end of <head>
.
Post a Comment for "Define Global CSS Classes Using JavaScript Or JQuery?"