国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript動態(tài)插入CSS的方法

2024-05-06 16:26:05
字體:
供稿:網(wǎng)友
這篇文章主要介紹了JavaScript動態(tài)插入CSS的方法,兩個步驟就可以實現(xiàn)JavaScript動態(tài)插入CSS
 

寫組件時有時想把一些組件特性相關(guān)的 CSS 樣式封裝在 JS 里,這樣更內(nèi)聚,改起來方便。JS 動態(tài)插入 CSS 兩個步驟:創(chuàng)建1、一個 style 對象
2、使用 stylesheet 的 insertRule 或 addRule 方法添加樣式

一、查看樣式表

先看下 document.styleSheets,隨意打開一個頁面

JavaScript動態(tài)插入CSS的方法

其中前三個是通過 link 標簽引入的 CSS 文件,第四個是通過 style 標簽內(nèi)聯(lián)在頁面里的 CSS。有如下屬性

JavaScript動態(tài)插入CSS的方法

 每一個 cssRule 又有如下屬性

JavaScript動態(tài)插入CSS的方法

其中的 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的方法,希望對大家的學習有所幫助。



注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 平顺县| 武邑县| 玉树县| 封开县| 兰溪市| 松潘县| 高要市| 左权县| 石城县| 吉安市| 隆昌县| 兰西县| 囊谦县| 衡阳县| 司法| 兴仁县| 安达市| 湛江市| 色达县| 巫溪县| 涡阳县| 永州市| 饶平县| 河池市| 桑日县| 札达县| 集安市| 习水县| 望奎县| 克山县| 蒙阴县| 易门县| 大关县| 咸阳市| 白城市| 应用必备| 古交市| 高安市| 常德市| 达拉特旗| 十堰市|