向當(dāng)前style sheet中插入一個(gè)新的style實(shí)現(xiàn)方法
更新時(shí)間:2013年04月01日 09:22:35 作者:
今天為了臨時(shí)解決頁(yè)面樣式問(wèn)題,為了方便,直接在這個(gè)公共的js里面向style sheet插入新的style rule,感興趣的朋友可以出納卡下哈
很少會(huì)插入一個(gè)新的style rule,今天為了臨時(shí)解決頁(yè)面樣式問(wèn)題,需要更新很多頁(yè)面的一些樣式,這些頁(yè)面都引用了一個(gè)公共的js,為了方便,直接在這個(gè)公共的js里面向style sheet插入新的style rule。
先看代碼:
/**
* Add a stylesheet rule to the document (may be better practice, however,
* to dynamically change classes, so style information can be kept in
* genuine styesheets (and avoid adding extra elements to the DOM))
* Note that an array is needed for declarations and rules since ECMAScript does
* not afford a predictable object iteration order and since CSS is
* order-dependent (i.e., it is cascading); those without need of
* cascading rules could build a more accessor-friendly object-based API.
* @param {Array} decls Accepts an array of JSON-encoded declarations
* @example
addStylesheetRules([
['h2', // Also accepts a second argument as an array of arrays instead
['color', 'red'],
['background-color', 'green', true] // 'true' for !important rules
],
['.myClass',
['background-color', 'yellow']
]
]);
*/
function addStylesheetRules (decls) {
var style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(document.createTextNode(''));
}
var s = document.styleSheets[document.styleSheets.length - 1];
for (var i=0, dl = decls.length; i < dl; i++) {
var j = 1, decl = decls[i], selector = decl[0], rulesStr = '';
if (Object.prototype.toString.call(decl[1][0]) === '[object Array]') {
decl = decl[1];
j = 0;
}
for (var rl=decl.length; j < rl; j++) {
var rule = decl[j];
rulesStr += rule[0] + ':' + rule[1] + (rule[2] ? ' !important' : '') + ';\n';
}
if (s.insertRule) {
s.insertRule(selector + '{' + rulesStr + '}', s.cssRules.length);
}
else { /* IE */
s.addRule(selector, rulesStr, -1);
}
}
}
addStylesheetRules(["div.content", ["color": "#000"], ["border-width","1px"], ["border-style", "solid"]])
執(zhí)行后當(dāng)前document的head標(biāo)簽內(nèi),多了一個(gè)style
<style>
div.content{color:#000;border:1px solid}
</style
知道怎么調(diào)用了吧,每次調(diào)用都會(huì)插入一個(gè)新的style,所以最好調(diào)用一次,插入多個(gè)rule
addStylesheetRules(
[selector, [attr, value], …],
[selector, [attr, value], …]
);
主要用到兩個(gè)方法:
標(biāo)準(zhǔn)方法:stylesheet.insertRule(rule, index)
rule:被插入的rule,如 div.content{color:#000}
index: 插入順序,先后順序會(huì)影響樣式的。從0開始
firefox、chrome、opera、safri、ie從ie9開始也支持這個(gè)方法
ie的stylesheet.addRule (selector, styleDef [, positionIndex]);
selector:如div.content
styleDef:如color:#000
positionIndex:默認(rèn)-1,插入到末尾
ie、safari、chrome支持這個(gè)方法
先看代碼:
復(fù)制代碼 代碼如下:
/**
* Add a stylesheet rule to the document (may be better practice, however,
* to dynamically change classes, so style information can be kept in
* genuine styesheets (and avoid adding extra elements to the DOM))
* Note that an array is needed for declarations and rules since ECMAScript does
* not afford a predictable object iteration order and since CSS is
* order-dependent (i.e., it is cascading); those without need of
* cascading rules could build a more accessor-friendly object-based API.
* @param {Array} decls Accepts an array of JSON-encoded declarations
* @example
addStylesheetRules([
['h2', // Also accepts a second argument as an array of arrays instead
['color', 'red'],
['background-color', 'green', true] // 'true' for !important rules
],
['.myClass',
['background-color', 'yellow']
]
]);
*/
function addStylesheetRules (decls) {
var style = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
if (!window.createPopup) { /* For Safari */
style.appendChild(document.createTextNode(''));
}
var s = document.styleSheets[document.styleSheets.length - 1];
for (var i=0, dl = decls.length; i < dl; i++) {
var j = 1, decl = decls[i], selector = decl[0], rulesStr = '';
if (Object.prototype.toString.call(decl[1][0]) === '[object Array]') {
decl = decl[1];
j = 0;
}
for (var rl=decl.length; j < rl; j++) {
var rule = decl[j];
rulesStr += rule[0] + ':' + rule[1] + (rule[2] ? ' !important' : '') + ';\n';
}
if (s.insertRule) {
s.insertRule(selector + '{' + rulesStr + '}', s.cssRules.length);
}
else { /* IE */
s.addRule(selector, rulesStr, -1);
}
}
}
復(fù)制代碼 代碼如下:
addStylesheetRules(["div.content", ["color": "#000"], ["border-width","1px"], ["border-style", "solid"]])
執(zhí)行后當(dāng)前document的head標(biāo)簽內(nèi),多了一個(gè)style
復(fù)制代碼 代碼如下:
<style>
div.content{color:#000;border:1px solid}
</style
知道怎么調(diào)用了吧,每次調(diào)用都會(huì)插入一個(gè)新的style,所以最好調(diào)用一次,插入多個(gè)rule
復(fù)制代碼 代碼如下:
addStylesheetRules(
[selector, [attr, value], …],
[selector, [attr, value], …]
);
主要用到兩個(gè)方法:
標(biāo)準(zhǔn)方法:stylesheet.insertRule(rule, index)
rule:被插入的rule,如 div.content{color:#000}
index: 插入順序,先后順序會(huì)影響樣式的。從0開始
firefox、chrome、opera、safri、ie從ie9開始也支持這個(gè)方法
ie的stylesheet.addRule (selector, styleDef [, positionIndex]);
selector:如div.content
styleDef:如color:#000
positionIndex:默認(rèn)-1,插入到末尾
ie、safari、chrome支持這個(gè)方法
您可能感興趣的文章:
相關(guān)文章
node.js Web應(yīng)用框架Express入門指南
這篇文章主要介紹了node.js Web應(yīng)用框架Express入門指南,從安裝到各種技術(shù)的應(yīng)用,都進(jìn)行了講解,是一篇不錯(cuò)的Express入門教程,需要的朋友可以參考下2014-05-05
Echarts地圖添加引導(dǎo)線效果(labelLine)
這篇文章主要介紹了Echarts地圖添加引導(dǎo)線效果(labelLine),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
JavaScript初學(xué)者需要了解10個(gè)小技巧
在之前的編程語(yǔ)言排行榜中,我們?cè)榻B過(guò)轉(zhuǎn)正在即的JavaScript語(yǔ)言,正如文章中闡明的那樣,JavaScript不僅是最具活力的腳本語(yǔ)言,還是是最有用的編程語(yǔ)言之一。2010-08-08
如何在uniapp項(xiàng)目中嵌套H5 頁(yè)面
在UniApp中可以通過(guò)使用 web-view 組件來(lái)嵌入H5頁(yè)面,大概思路是在該頁(yè)面的template部分添加web-view組件,設(shè)置src屬性為所需嵌入的H5頁(yè)面地址,感興趣的朋友跟隨小編一起看看吧2024-02-02
JavaScript實(shí)現(xiàn)篩選數(shù)組
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)篩選數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
利用JavaScript實(shí)現(xiàn)檢測(cè)用戶是否在線功能
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript實(shí)現(xiàn)檢測(cè)用戶是否在線功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
bootstrap-treeview實(shí)現(xiàn)多級(jí)樹形菜單 后臺(tái)JSON格式如何組織?
這篇文章主要介紹了bootstrap-treeview實(shí)現(xiàn)多級(jí)樹形菜單,后臺(tái)JSON格式如何組織,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07

