首页 相关文章 Javascript动态创建 style 节点

Javascript动态创建 style 节点

有很多提供动态创建 style 节点的方法,但是大多数都仅限于外部的 css 文件。如何能使用程序生成的字符串动态创建 style 节点,我搞了2个小时。

静态外部 css 文件语法:

@import url(style.css);

动态外部 css 文件加载的方法有如下:

第一种:

var style = document.createElement(’link’);
style.href = ’style.css’;
style.rel = ’stylesheet’;
style.type = ‘text/css’;
document.getElementsByTagName(’HEAD’).item(0).appendChild(style);

第二种简单:

document.createStyleSheet(style.css);

...[ 查看全文 ]

2016-02-19 标签:

Javascript动态创建 style 节点的相关文章

手机页面
收藏网站 回到头部