使用JavaScript或jQuery定义全局CSS类? [英] Define global CSS classes using JavaScript or jQuery?
本文介绍了使用JavaScript或jQuery定义全局CSS类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法使用JavaScript或jQuery设置全局类的CSS?也就是说,将 .my_class {foo:bar}
附加到页面的< style />
标记中?
Is there a way to set the CSS of global classes using JavaScript or jQuery? That is, append .my_class { foo:bar }
to the <style/>
tag of the page?
推荐答案
纯javascript -
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);
这篇关于使用JavaScript或jQuery定义全局CSS类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文