Change CSS class properties with jQuery

后端 未结 8 1615
心在旅途
心在旅途 2020-11-29 04:59

Is there a way to change the properties of a CSS class, not the element properties, using jQuery?

This is a practical example:

I have a div with class

8条回答
  •  再見小時候
    2020-11-29 05:42

    Here's a bit of an improvement on the excellent answer provided by Mathew Wolf. This one appends the main container as a style tag to the head element and appends each new class to that style tag. a little more concise and I find it works well.

    function changeCss(className, classValue) {
        var cssMainContainer = $('#css-modifier-container');
    
        if (cssMainContainer.length == 0) {
            var cssMainContainer = $('');
            cssMainContainer.appendTo($('head'));
        }
    
        cssMainContainer.append(className + " {" + classValue + "}\n");
    }
    

提交回复
热议问题