[英]Define global CSS classes using JavaScript or jQuery?
Is there a way to set the CSS of global classes using JavaScript or jQuery? 有没有办法使用JavaScript或jQuery设置全局类的CSS? That is, append
.my_class { foo:bar }
to the <style/>
tag of the page? 也就是说,将
.my_class { foo:bar }
附加到页面的<style/>
标签上?
Pure javascript - 纯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);
Yes, you can do that in jQuery: 是的,你可以在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>
. 它只需在
<head>
的末尾添加<style>
标记即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.