How to dynamically create CSS class in JavaScript and apply?

后端 未结 15 1024
长情又很酷
长情又很酷 2020-11-22 02:57

I need to create a CSS stylesheet class dynamically in JavaScript and assign it to some HTML elements like - div, table, span, tr, etc and to some controls like asp:Textbox,

15条回答
  •  遇见更好的自我
    2020-11-22 03:34

    function createCSSClass(selector, style, hoverstyle) 
    {
        if (!document.styleSheets) 
        {
            return;
        }
    
        if (document.getElementsByTagName("head").length == 0) 
        {
    
            return;
        }
        var stylesheet;
        var mediaType;
        if (document.styleSheets.length > 0) 
        {
            for (i = 0; i < document.styleSheets.length; i++) 
            {
                if (document.styleSheets[i].disabled) 
                {
                    continue;
                }
                var media = document.styleSheets[i].media;
                mediaType = typeof media;
    
                if (mediaType == "string") 
                {
                    if (media == "" || (media.indexOf("screen") != -1)) 
                    {
                        styleSheet = document.styleSheets[i];
                    }
                } 
                else if (mediaType == "object") 
                {
                    if (media.mediaText == "" || (media.mediaText.indexOf("screen") != -1)) 
                    {
                        styleSheet = document.styleSheets[i];
                    }
                }
    
                if (typeof styleSheet != "undefined") 
                {
                    break;
                }
            }
        }
    
        if (typeof styleSheet == "undefined") {
            var styleSheetElement = document.createElement("style");
            styleSheetElement.type = "text/css";
            document.getElementsByTagName("head")[0].appendChild(styleSheetElement);
            for (i = 0; i < document.styleSheets.length; i++) {
                if (document.styleSheets[i].disabled) {
                    continue;
                }
                styleSheet = document.styleSheets[i];
            }
    
            var media = styleSheet.media;
            mediaType = typeof media;
        }
    
        if (mediaType == "string") {
            for (i = 0; i < styleSheet.rules.length; i++) 
            {
                if (styleSheet.rules[i].selectorText.toLowerCase() == selector.toLowerCase()) 
                {
                    styleSheet.rules[i].style.cssText = style;
                    return;
                }
            }
    
            styleSheet.addRule(selector, style);
        }
        else if (mediaType == "object") 
        {
            for (i = 0; i < styleSheet.cssRules.length; i++) 
            {
                if (styleSheet.cssRules[i].selectorText.toLowerCase() == selector.toLowerCase()) 
                {
                    styleSheet.cssRules[i].style.cssText = style;
                    return;
                }
            }
    
            if (hoverstyle != null) 
            {
                styleSheet.insertRule(selector + "{" + style + "}", 0);
                styleSheet.insertRule(selector + ":hover{" + hoverstyle + "}", 1);
            }
            else 
            {
                styleSheet.insertRule(selector + "{" + style + "}", 0);
            }
        }
    }
    
    
    
    
    
    createCSSClass(".modalPopup  .header",
                                     " background-color: " + lightest + ";" +
                                      "height: 10%;" +
                                      "color: White;" +
                                      "line-height: 30px;" +
                                      "text-align: center;" +
                                      " width: 100%;" +
                                      "font-weight: bold; ", null);
    

提交回复
热议问题