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

首頁 > 編程 > JavaScript > 正文

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

2019-11-20 11:02:26
字體:
供稿:網(wǎng)友

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

一、查看樣式表

先看下 document.styleSheets,隨意打開一個(gè)頁面

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

 每一個(gè) cssRule 又有如下屬性

其中的 cssText 正是寫在 style 的源碼。 

二、動(dòng)態(tài)插入 CSS
首先,需要?jiǎng)?chuàng)建一個(gè) style 對(duì)象,返回其 stylesheet 對(duì)象

/* * 創(chuàng)建一個(gè) style, 返回其 stylesheet 對(duì)象 * 注意: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 如下

/* * 動(dòng)態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會(huì)覆蓋靠前的 */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);  }}

需要注意,標(biāo)準(zhǔn)瀏覽器支持 insertRule, IE低版本則支持 addRule。
完整代碼如下

/* * 動(dòng)態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會(huì)覆蓋靠前的 */var addCssRule = function() { // 創(chuàng)建一個(gè) style, 返回其 stylesheet 對(duì)象 // 注意: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 對(duì)象 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);   } }}();


如果只支持移動(dòng)端或現(xiàn)代瀏覽器,可以去掉低版本IE判斷的代碼

/* * 動(dòng)態(tài)添加 CSS 樣式 * @param selector {string} 選擇器 * @param rules {string} CSS樣式規(guī)則 * @param index {number} 插入規(guī)則的位置, 靠后的規(guī)則會(huì)覆蓋靠前的,默認(rèn)在后面插入 */var addCssRule = function() { // 創(chuàng)建一個(gè) style, 返回其 stylesheet 對(duì)象 function createStyleSheet() {  var style = document.createElement('style');  style.type = 'text/css';  document.head.appendChild(style);  return style.sheet; }  // 創(chuàng)建 stylesheet 對(duì)象 var sheet = createStyleSheet();  // 返回接口函數(shù) return function(selector, rules, index) {  index = index || 0;  sheet.insertRule(selector + "{" + rules + "}", index); }}();

以上就是JavaScript動(dòng)態(tài)插入CSS的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 虎林市| 福泉市| 婺源县| 河池市| 靖安县| 大关县| 府谷县| 南部县| 汶川县| 乌兰县| 隆昌县| 乌海市| 鄂尔多斯市| 西昌市| 通城县| 杭锦旗| 寻乌县| 苍山县| 黄大仙区| 武胜县| 林口县| 偃师市| 大英县| 伊吾县| 德令哈市| 冀州市| 新河县| 富川| 侯马市| 航空| 牟定县| 延津县| 滦平县| 玉田县| 安塞县| 三台县| 左贡县| 铜川市| 沐川县| 三明市| 长葛市|