日期:2014-05-17  浏览次数:20635 次

js操作cssRule的增删改
initSize: function(){
                var sheet = null;
                for (var i = 0, len = document.styleSheets.length; i < len; i++) {
                    sheet = document.styleSheets[i];
                    if (sheet.href.indexOf('transitions') != -1) {
                        var rules = sheet.rules || sheet.cssRules;//取得规则列表
                        //修改样式表中的样式规则
                        var rule = rules[0];//取得第一条
                        rule.style.width = options.imageWidth + "px";
rule.style.height = options.imageHeight + "px";
rule = rules[1];//取得第2条
                        rule.style.width = options.imageWidth + "px";
rule.style.height = options.imageHeight + 80 + "px";
//删除前面两条rules。
//                        deleteRule(sheet, 0);
//                        deleteRule(sheet, 1);
//                        insertRule(sheet, ".imageSize", "width:" + options.imageWidth + ";" + "height:" + options.imageHeight + ";", 0);
//                        insertRule(sheet, ".wrapperSize", "width:" + options.imageWidth + ";" + "height:" + options.imageHeight + 80 + ";", 1);
                        console.log(sheet);
break;
                    }
                }
                function insertRule(sheet, selectorText, cssText, position){
                    if (sheet.insertRule) {
                        sheet.insertRule(selectorText + "{" + cssText + "}", position);
                    }
                    else
                        if (sheet.addRule) { //仅对IE有效