JavaScript動態(tài)插入CSS的方法
寫組件時有時想把一些組件特性相關的 CSS 樣式封裝在 JS 里,這樣更內(nèi)聚,改起來方便。JS 動態(tài)插入 CSS 兩個步驟:創(chuàng)建1、一個 style 對象
2、使用 stylesheet 的 insertRule 或 addRule 方法添加樣式
一、查看樣式表
先看下 document.styleSheets,隨意打開一個頁面
其中前三個是通過 link 標簽引入的 CSS 文件,第四個是通過 style 標簽內(nèi)聯(lián)在頁面里的 CSS。有如下屬性
每一個 cssRule 又有如下屬性
其中的 cssText 正是寫在 style 的源碼。
二、動態(tài)插入 CSS
首先,需要創(chuàng)建一個 style 對象,返回其 stylesheet 對象
/* * 創(chuàng)建一個 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; }
添加函數(shù) addCssRule 如下
/* * 動態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會覆蓋靠前的 */ 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。
完整代碼如下
/* * 動態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會覆蓋靠前的 */ var addCssRule = function() { // 創(chuàng)建一個 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; } // 創(chuàng)建 stylesheet 對象 var sheet = createStyleSheet(); // 返回接口函數(shù) 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); } } }();
如果只支持移動端或現(xiàn)代瀏覽器,可以去掉低版本IE判斷的代碼
/* * 動態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會覆蓋靠前的,默認在后面插入 */ var addCssRule = function() { // 創(chuàng)建一個 style, 返回其 stylesheet 對象 function createStyleSheet() { var style = document.createElement('style'); style.type = 'text/css'; document.head.appendChild(style); return style.sheet; } // 創(chuàng)建 stylesheet 對象 var sheet = createStyleSheet(); // 返回接口函數(shù) return function(selector, rules, index) { index = index || 0; sheet.insertRule(selector + "{" + rules + "}", index); } }();
以上就是JavaScript動態(tài)插入CSS的方法,希望對大家的學習有所幫助。
- 深入理解javascript動態(tài)插入技術
- JavaScript動態(tài)插入script的基本思路及實現(xiàn)函數(shù)
- 得到文本框選中的文字,動態(tài)插入文字的js代碼
- 如何讓動態(tài)插入的javascript腳本代碼跑起來。
- Javascript基于AJAX回調(diào)函數(shù)傳遞參數(shù)實例分析
- js自定義回調(diào)函數(shù)
- 談談JavaScript自定義回調(diào)函數(shù)
- js的回調(diào)函數(shù)詳解
- 告訴你什么是javascript的回調(diào)函數(shù)
- js中回調(diào)函數(shù)的學習筆記
- JS動態(tài)插入并立即執(zhí)行回調(diào)函數(shù)的方法
相關文章
javascript實現(xiàn)tabs選項卡切換效果(自寫原生js)
常用的頁面效果有彈出層效果,無縫滾動效果,選項卡切換效果,接下來與大家分享一款自己用原生javascript寫的選項卡切換效果,感興趣的朋友可以參考下哈2013-03-03基于JavaScript實現(xiàn)動態(tài)添加刪除表格的行
又一個動態(tài)控制表格的效果,用JavaScript動態(tài)生成表格行、表格列,以及還可動態(tài)刪除這些行列,行等,運行代碼后,點擊對應的功能按鈕,即可實現(xiàn)對應的表格操作功能,接下來通過代碼實例給大家介紹JavaScript實現(xiàn)動態(tài)添加刪除表格的行,需要的朋友參考下2016-02-02JavaScript 開發(fā)規(guī)范要求(圖文并茂)
作為一名開發(fā)人員(WEB前端JavaScript開發(fā)),不規(guī)范的開發(fā)不僅使日后代碼維護變的困難,同時也不利于團隊的合作,通常還會帶來代碼安全以及執(zhí)行效率上的問題。2010-06-06H5如何實現(xiàn)喚起APP及調(diào)試bug解決
這篇文章主要為大家介紹了H5如何實現(xiàn)喚起APP及調(diào)試bug解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05