zoukankan      html  css  js  c++  java
  • 2015-02-09——js笔记

    示例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];
                                }
                            }
                        }
                    }
                }

     

  • 相关阅读:
    JAVA-初步认识-第五章-数组-常见操作-最值2
    JAVA-初步认识-第五章-数组-常见操作-最值
    JAVA-初步认识-第五章-数组-常见操作-遍历
    JAVA-初步认识-第五章-数组-第二种定义格式
    JAVA-初步认识-第四章-数组-常见问题
    JAVA-初步认识-第四章-内存图解
    日期加1程序
    发生了COMException 异常来自 HRESULT:0x80040228
    设置窗体的可见性无效
    DotNetBar RibbonControl 控件动态添加项
  • 原文地址:https://www.cnblogs.com/bugong/p/4281422.html
Copyright © 2011-2022 走看看