How do you add CSS with Javascript?

前端 未结 14 2626
旧时难觅i
旧时难觅i 2020-11-22 17:18

How do you add CSS rules (eg strong { color: red }) by use of Javascript?

相关标签:
14条回答
  • 2020-11-22 17:44

    You can add classes or style attributes on an element by element basis.

    For example:

    <a name="myelement" onclick="this.style.color='#FF0';">text</a>
    

    Where you could do this.style.background, this.style.font-size, etc. You can also apply a style using this same method ala

    this.className='classname';
    

    If you want to do this in a javascript function, you can use getElementByID rather than 'this'.

    0 讨论(0)
  • 2020-11-22 17:45

    You can also do this using DOM Level 2 CSS interfaces (MDN):

    var sheet = window.document.styleSheets[0];
    sheet.insertRule('strong { color: red; }', sheet.cssRules.length);
    

    ...on all but (naturally) IE8 and prior, which uses its own marginally-different wording:

    sheet.addRule('strong', 'color: red;', -1);
    

    There is a theoretical advantage in this compared to the createElement-set-innerHTML method, in that you don't have to worry about putting special HTML characters in the innerHTML, but in practice style elements are CDATA in legacy HTML, and ‘<’ and ‘&’ are rarely used in stylesheets anyway.

    You do need a stylesheet in place before you can started appending to it like this. That can be any existing active stylesheet: external, embedded or empty, it doesn't matter. If there isn't one, the only standard way to create it at the moment is with createElement.

    0 讨论(0)
  • 2020-11-22 17:49

    The simple-and-direct approach is to create and add a new style node to the document.

    // Your CSS as text
    var styles = `
        .qwebirc-qui .ircwindow div { 
            font-family: Georgia,Cambria,"Times New Roman",Times,serif;
            margin: 26px auto 0 auto;
            max-width: 650px;
        }
        .qwebirc-qui .lines {
            font-size: 18px;
            line-height: 1.58;
            letter-spacing: -.004em;
        }
    
        .qwebirc-qui .nicklist a {
            margin: 6px;
        }
    `
    
    var styleSheet = document.createElement("style")
    styleSheet.type = "text/css"
    styleSheet.innerText = styles
    document.head.appendChild(styleSheet)
    
    0 讨论(0)
  • 2020-11-22 17:53

    YUI just recently added a utility specifically for this. See stylesheet.js here.

    0 讨论(0)
  • 2020-11-22 17:55

    use .css in Jquery like $('strong').css('background','red');

    $('strong').css('background','red');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <strong> Example
    </strong> 

    0 讨论(0)
  • 2020-11-22 17:56

    This is my solution to add a css rule at the end of the last style sheet list:

    var css = new function()
    {
        function addStyleSheet()
        {
            let head = document.head;
            let style = document.createElement("style");
    
            head.appendChild(style);
        }
    
        this.insert = function(rule)
        {
            if(document.styleSheets.length == 0) { addStyleSheet(); }
    
            let sheet = document.styleSheets[document.styleSheets.length - 1];
            let rules = sheet.rules;
    
            sheet.insertRule(rule, rules.length);
        }
    }
    
    css.insert("body { background-color: red }");
    
    0 讨论(0)
提交回复
热议问题