首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

js操作cssRule的增删节

2012-10-21 
js操作cssRule的增删改initSize: function(){var sheet nullfor (var i 0, len document.styleShee

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有效
                            sheet.addRule(selectorText, cssText, position);
                        }
                }
                function deleteRule(sheet, index){
                    if (sheet.deleteRule) {
                        sheet.deleteRule(index);
                    }
                    else
                        if (sheet.removeRule) { //仅对IE有效
                            sheet.removeRule(index);
                        }
                }
            },

热点排行