示例1:

   增加样式表

   示例代码:

function addStylesheet(url, media) {
                var link = document.createElement('link');
                link.setAttribute('rel', 'stylesheet');
                link.setAttribute('type', 'text/css');
                link.setAttribute('meida', media);
                link.setAttribute('href', url);
                document.getElementsByTagName('head')[0].appendChild(link);
            }

示例2:

   获取样式表

   示例代码:

function getStylesheet(url, media) {
                var sheets = [];
                for (var i = 0; i < document.styleSheets.length; i++) {
                    if (url && document.styleSheets[i].href.indexOf(url) === -1 ) {
                        continue;
                    }
                    if (media) {
                        var sheetMedia;
                        if (document.styleSheets[i].media.mediaText) {
                            sheetMedia = document.styleSheets[i].media.mediaText;
                        } else {
                            sheetMedia = document.styleSheets[i].media;
                        }
                        if (media !== sheetMedia) {
                            continue;
                        }
                    }
                    sheets.push(document.styleSheets[i]);
                }
            }

示例3:

   删除样式表

   示例代码:

function removeStylesheet(url, media) {
                var sheets = getStylesheet(url, media);
                for (var i = 0; i < sheets.length; i++) {
                    var node = sheets[i].ownerNode || sheets[i].owningElement;
                    sheets[i].disabled = true;
                    node.parentNode.removeChild(node);
                }
            }

示例4:

   增加一条CSS规则

   示例代码:

function addCssRule(selector, styles, index, url, meida) {
                var declaration = '';
                for (property in styles) {
                    if (!styles.hasOwnProperty(property)) {
                        continue;
                    }
                    declaration += property + ':' + styles[property] + ';';
                }
                var styleSheets = (typeof url === 'array') ? url : getStylesheet(url, media);
                var newIndex;
                for (var i = 0; i < styleSheets.length; i++) {
                    if (styleSheets[i].insertRule) {//DOM2
                        newIndex = index > 0 ? index : styleSheets[i].cssRules;
                        styleSheets[i].insertRule(selector + '{' + declaration + '}', newIndex);
                    } else if (styleSheets[i].addRule) {//MSIE
                        newIndex = index >= 0 ? index : -1;
                        styleSheets[i].addRule(selector, declaration, newIndex);
                    }
                }
            }

示例5:

   编辑一条css规则

   示例代码:

function editCssRule(selector, styles, url, media) {
                var styleSheets = (typeof url === 'array') ? url : getStylesheet(url, media);
                for (var i = 0; i < styleSheets.length; i++) {
                    var rules = styleSheets[i].cssRules || styleSheets[i].rules;
                    selector = selector.toUpperCase();
                    for (var j = 0; j < rules.length; j++) {
                        if (rules[j].selectorText.toUpperCase() === selector ) {
                            for (property in styles) {
                                if (!styles.hasOwnProperty(property)) {
                                    continue;
                                }
                                rules[j].style[camelize(property)] = styles[property];
                            }
                        }
                    }
                }
            }

 

最新文章

  1. Vim基础操作
  2. 气象API(2)
  3. arch linux 新版安装(转)
  4. js词法作用域规则
  5. 30天轻松学习javaweb_Range实现断点续传
  6. fedora23开发环境搭建手册
  7. SQL查询表字段、字段说明、类型、长度、主键
  8. Qt 中文乱码解决大全
  9. 6、Web应用程序中的安全向量 -- customErrors(适当的错误报告和堆栈跟踪)
  10. xml的生成和发送
  11. Java 继承、抽象、接口
  12. Unity学习资料
  13. SpringBoot(四):banner的控制
  14. iOS控制反转(IoC)与依赖注入(DI)的实现
  15. jsp篇 之 jsp页面中的路径问题
  16. 在 Apex 中使用合并统计查询
  17. 4.10Python数据处理篇之Matplotlib系列(十)---文本的显示
  18. day25 Python __setattr__
  19. Why is 'x' in ('x',) faster than 'x' == 'x'?
  20. Linux下统计局域网流量

热门文章

  1. PNP管理器简析--基于ReactOS0.33
  2. Nginx-安装依赖及配置详解
  3. 在Window系统中使用Redis缓存策略
  4. UDP与TCP报文格式,字段意义
  5. wget 命令
  6. protobuf 在win10系统如何编译jar包
  7. 219. Contains Duplicate II【easy】
  8. linux清空文件夹命令问题
  9. 如何上传package到pypi
  10. spring 第一篇(1-1):让java开发变得更简单(上)