How to dynamically create CSS class in JavaScript and apply?

后端 未结 15 1025
长情又很酷
长情又很酷 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:41

    Here is Vishwanath's solution slightly rewritten with comments :

    function setStyle(cssRules, aSelector, aStyle){
        for(var i = 0; i < cssRules.length; i++) {
            if(cssRules[i].selectorText && cssRules[i].selectorText.toLowerCase() == aSelector.toLowerCase()) {
                cssRules[i].style.cssText = aStyle;
                return true;
            }
        }
        return false;
    }
    
    function createCSSSelector(selector, style) {
        var doc = document;
        var allSS = doc.styleSheets;
        if(!allSS) return;
    
        var headElts = doc.getElementsByTagName("head");
        if(!headElts.length) return;
    
        var styleSheet, media, iSS = allSS.length; // scope is global in a function
        /* 1. search for media == "screen" */
        while(iSS){ --iSS;
            if(allSS[iSS].disabled) continue; /* dont take into account the disabled stylesheets */
            media = allSS[iSS].media;
            if(typeof media == "object")
                media = media.mediaText;
            if(media == "" || media=='all' || media.indexOf("screen") != -1){
                styleSheet = allSS[iSS];
                iSS = -1;   // indication that media=="screen" was found (if not, then iSS==0)
                break;
            }
        }
    
        /* 2. if not found, create one */
        if(iSS != -1) {
            var styleSheetElement = doc.createElement("style");
            styleSheetElement.type = "text/css";
            headElts[0].appendChild(styleSheetElement);
            styleSheet = doc.styleSheets[allSS.length]; /* take the new stylesheet to add the selector and the style */
        }
    
        /* 3. add the selector and style */
        switch (typeof styleSheet.media) {
        case "string":
            if(!setStyle(styleSheet.rules, selector, style));
                styleSheet.addRule(selector, style);
            break;
        case "object":
            if(!setStyle(styleSheet.cssRules, selector, style));
                styleSheet.insertRule(selector + "{" + style + "}", styleSheet.cssRules.length);
            break;
        }
    

提交回复
热议问题