Skip to content

Instantly share code, notes, and snippets.

@larsberg
Forked from zz85/cssRule.js
Created June 27, 2014 16:43
Show Gist options
  • Save larsberg/f96599fb973c54b34881 to your computer and use it in GitHub Desktop.
Save larsberg/f96599fb973c54b34881 to your computer and use it in GitHub Desktop.
if (!document.styleSheets.length) document.head.appendChild(document.createElement('style'));
var sheet = document.styleSheets[document.styleSheets.length - 1];
var rules = {};
function cssRule(selector, styles) {
var index;
if (selector in rules) {
index = rules[selector];
sheet.deleteRule(index);
} else {
index = rules[selector] = sheet.cssRules.length;
}
sheet.insertRule(selector + " {" + styles + "}", index);
// Don't support for <IE9 sheet.addRule, sorry, ha!
}
cssRule('body', 'background-color: black;')
cssRule('body', 'background-color: white;')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment