1 //add Addcssrule ('. BCD ', {' Color ': ' Red ', ' font-weight ': ' Bold ', ' font-size ': ' 12px '},document.stylesheets[1]);2 functionAddcssrule (selector,styles,stylesheet,index) {3 varSheets = StyleSheetinstanceofArray?styleSheet: [StyleSheet],4style = ' ',5Declaration = ' ';6 for(varProinchstyles) {7 if(Styles.hasownproperty (Pro)) {8Declaration + = (Pro + ': ' + styles[pro] + '; ');9 }Ten } Onestyle = selector + ' {' + declaration + '} '; A for(vari = 0,j = Sheets.length; I < j;i++){ - if(sheets[i].insertrule) { -index = index >= 0? index:sheets[i][' Cssrules '].length; the Sheets[i].insertrule (style,index); -}Else if(Sheets[i].addrule) {//IE -index = index >= 0? Index:-1;//-1 for the end - Sheets[i].addrule (selector,declaration,index); + } - } + } A at //Modify Editcssrule ('. abc ', {' font-weight ': ' Bold ', ' font-size ': ' 14px '},document.stylesheets[1]); - functionEditcssrule (selector,styles,stylesheet) { - varSheets = StyleSheetinstanceofArray?styleSheet: [StyleSheet]; -selector = Selector.touppercase ();//IE9 The following label selectors default is uppercase here unified under - //selector can be problematic for case sensitive - for(vari = 0,j = Sheets.length; I < j;i++){ in varRules = sheets[i][' Cssrules ' | | sheets[i][' rules '];//IE is rules - if(!rules) {Continue;} to for(varm = 0,n = Rules.length;m < n;m++){ + if(rules[m][' Selectortext '].touppercase () = =selector) { - for(varProinchstyles) { the if(Styles.hasownproperty (Pro)) { *Rules[m].style[csscamilize (PRO)] =Styles[pro]; $ }Panax Notoginseng } - } the } + } A } the + //del delcssrule ('. BCD ', document.stylesheets[1]); - functionDelcssrule (selector,stylesheet,index) { $ varSheets = StyleSheetinstanceofArray?styleSheet: [StyleSheet]; $selector =selector.touppercase (); - for(vari = 0,j = Sheets.length; I < j;i++){ - varRules = sheets[i][' Cssrules ' | | sheets[i][' rules '];//IE is rules the if(Index >=0 && Index <rules.length) { -Sheets[i].deleterule?Sheets[i].deleterule (Index): Sheets[i].removerule (index);Wuyi}Else if(selector) { the for(varm = 0,n = Rules.length;m < n;m++){ - if(rules[m][' Selectortext '].touppercase () = =selector) { WuSheets[i].deleterule? Sheets[i].deleterule (M): Sheets[i].removerule (m);//there may be more than one selector all deleted - } About } $}Else{ - return false; - } - } A }; + the functioncsscamilize (str) { - returnStr.replace (/-(\w)/g,function($1,$2){ $ return$. toUpperCase (); the }); the};
View Code
JavaScript manipulating CSS Rule