JavaScript 动态插入 CSS
写组件时有时想把一些组件特性相关的 CSS 样式封装在 JS 里,这样更内聚,改起来方便。JS 动态插入 CSS 两个步骤就可以
- 创建一个 style 对象
- 使用 stylesheet 的 insertRule 或 addRule 方法添加样式
一、查看样式表
先看下 document.styleSheets,随意打开一个页面
其中前三个是通过 link 标签引入的 CSS 文件,第四个是通过 style 标签内联在页面里的 CSS。有如下属性
每一个 cssRule 又有如下属性
其中的 cssText 正是写在 style 的源码。
二、动态插入 CSS
首先,需要创建一个 style 对象,返回其 stylesheet 对象
1 2 3 4 5 6 7 8 9 10 11 | /* * 创建一个 style, 返回其 stylesheet 对象 * 注意:IE6/7/8中使用 style.stylesheet,其它浏览器 style.sheet */ function createStyleSheet() { var head = document.head || document.getElementsByTagName( 'head' )[0]; var style = document.createElement( 'style' ); style.type = 'text/css' ; head.appendChild(style); return style.sheet ||style.styleSheet; } |
添加函数 addCssRule 如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /* * 动态添加 CSS 样式 * @param selector {string} 选择器 * @param rules {string} CSS样式规则 * @param index {number} 插入规则的位置, 靠后的规则会覆盖靠前的 */ function addCssRule(selector, rules, index) { index = index || 0; if (sheet.insertRule) { sheet.insertRule(selector + "{" + rules + "}" , index); } else if (sheet.addRule) { sheet.addRule(selector, rules, index); } } |
需要注意,标准浏览器支持 insertRule, IE低版本则支持 addRule。
完整代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* * 动态添加 CSS 样式 * @param selector {string} 选择器 * @param rules {string} CSS样式规则 * @param index {number} 插入规则的位置, 靠后的规则会覆盖靠前的 */ var addCssRule = function () { // 创建一个 style, 返回其 stylesheet 对象 // 注意:IE6/7/8中使用 style.stylesheet,其它浏览器 style.sheet function createStyleSheet() { var head = document.head || document.getElementsByTagName( 'head' )[0]; var style = document.createElement( 'style' ); style.type = 'text/css' ; head.appendChild(style); return style.sheet ||style.styleSheet; } // 创建 stylesheet 对象 var sheet = createStyleSheet(); // 返回接口函数 return function (selector, rules, index) { index = index || 0; if (sheet.insertRule) { sheet.insertRule(selector + "{" + rules + "}" , index); } else if (sheet.addRule) { sheet.addRule(selector, rules, index); } } }(); |
如果只支持移动端或现代浏览器,可以去掉低版本IE判断的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /* * 动态添加 CSS 样式 * @param selector {string} 选择器 * @param rules {string} CSS样式规则 * @param index {number} 插入规则的位置, 靠后的规则会覆盖靠前的,默认在后面插入 */ var addCssRule = function () { // 创建一个 style, 返回其 stylesheet 对象 function createStyleSheet() { var style = document.createElement( 'style' ); style.type = 'text/css' ; document.head.appendChild(style); return style.sheet; } // 创建 stylesheet 对象 var sheet = createStyleSheet(); // 返回接口函数 return function (selector, rules, index) { index = index || 0; sheet.insertRule(selector + "{" + rules + "}" , index); } }(); |
在线DEMO:http://snandy.github.io/lib/func/addCssRule.html
相关:
https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet
https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
2014-10-11 顺序语句
2011-10-11 直接选择排序
2011-10-11 冒泡排序(交换)