Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's something that will create a CSS class that will be available everywhere and apply it to a jQuery object. This uses the same basic technique as mentioned by MooGoo but is fleshed out into a fully functional piece of code:</p> <pre><code>(function() { var addRule; if (typeof document.styleSheets != "undefined" &amp;&amp; document.styleSheets) { addRule = function(selector, rule) { var styleSheets = document.styleSheets, styleSheet; if (styleSheets &amp;&amp; styleSheets.length) { styleSheet = styleSheets[styleSheets.length - 1]; if (styleSheet.addRule) { styleSheet.addRule(selector, rule) } else if (typeof styleSheet.cssText == "string") { styleSheet.cssText = selector + " {" + rule + "}"; } else if (styleSheet.insertRule &amp;&amp; styleSheet.cssRules) { styleSheet.insertRule(selector + " {" + rule + "}", styleSheet.cssRules.length); } } } } else { addRule = function(selector, rule, el, doc) { el.appendChild(doc.createTextNode(selector + " {" + rule + "}")); }; } var createCssClass = function(className, cssProps, doc) { doc = doc || document; var head = doc.getElementsByTagName("head")[0]; if (head &amp;&amp; addRule) { var selector = "*." + className; var ruleBits = []; for (var i in cssProps) { if (cssProps.hasOwnProperty(i)) { ruleBits.push(i + ":" + cssProps[i] + ";"); } } var rule = ruleBits.join(""); var styleEl = doc.createElement("style"); styleEl.type = "text/css"; styleEl.media = "screen"; head.appendChild(styleEl); addRule(selector, rule, styleEl, doc); styleEl = null; } }; jQuery.fn.createAndApplyCssClass = function(className, cssProps) { createCssClass(className, cssProps, document); this.addClass(className); }; })(); $("#someelement").createAndApplyCssClass("test", { "background-color": "green", "color" : "white" }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload