How to dynamically change the style tag using JavaScript?

前端 未结 9 2081
走了就别回头了
走了就别回头了 2021-01-01 21:50

I want to change the style tag after generating contents. How can I add style to the style tag? I tried using:

document.getElementById(\'style\').appendChil         


        
相关标签:
9条回答
  • 2021-01-01 22:36

    Adding onto @AtheistP3ace's answer, here is a function that uses pure JavaScript to change the content of a style block:

    // Change the CSS in the style section.
    // Property is the name of the class or id E.G. ".mdc-card" or "#main".
    // Style is the name of the CSS style. E.G. "Background".
    // Value is the setting that the style will use, E.G. "Red"
    // WARNING: STOPS AND EXECUTES ON THE FIRST MATCH DOES NOT PROCESS MORE AFTER THAT!!!
    function changeCSS(property, style, value, cssSectionID) {
        if (cssSectionID === undefined) {
            cssSectionID = "mainCSS";
        }
        // Get the current CSS sheet.
        var mainCSS = document.getElementById("mainCSS");
        var changeStatus = false;
        // Refine the stylesheet into a more easily processed form.
        // It is done here as making it part of the variable definition produces issues where you still need to call the sheet property.
        mainCSS = mainCSS.sheet;
    
        // Only execute if the user has specified values for each of the required parameters.
        if (property !== undefined && style !== undefined && value !== undefined) {
            // Loop through all of the CSS Properties until the specified property is found.
            for (var index = 0; index < mainCSS.cssRules.length; index++) {
    
                // Only apply the value of the property matches.
                if (mainCSS.cssRules[index].selectorText === property.toString()) {
                    // Apply the specified property value to the.
                    mainCSS.cssRules[index].style[style.toString()] = value.toString();
    
                    // Sets the exit status and breaks the loop's execution.
                    changeStatus = true;
                    break;
                }
            }   
        }
        // Returns the result of the operation. True being successful and false being unsuccessful.
        return changeStatus;
    }
    
    0 讨论(0)
  • 2021-01-01 22:38

    I tossed together a quick example at http://jsfiddle.net/fttS5/1/ .

    You can attach an id to the style tags just like you would any other element in HTML.

    <style id="style"></style>

    Now you can take the code you tried with appendChild and add one quick change to it

    document.getElementById('style').appendChild(document.createTextNode(styleContents));

    This should do the trick. Good Luck.

    You can also just use the innerHTML method listed as the answer below mine.

    0 讨论(0)
  • 2021-01-01 22:40

    You are probably trying to add css to the style tag. This can be accomplished using:

    document.getElementsByTagName('style')[0].innerHTML=""; //some css goes here
    

    You can also use:

    document.styleSheets[0].cssText = ""; //some css goes here
    
    0 讨论(0)
提交回复
热议问题