dynamicCss.js 530 B

12345678910111213141516171819202122
  1. class DynamicCss {
  2. constructor() {
  3. this.cssNodes = {};
  4. }
  5. replace(name, cssText) {
  6. const style = document.createElement('style');
  7. style.type = 'text/css';
  8. style.innerHTML = cssText;
  9. const parent = document.getElementsByTagName('head')[0];
  10. if (this.cssNodes[name]) {
  11. parent.removeChild(this.cssNodes[name]);
  12. delete this.cssNodes[name];
  13. }
  14. this.cssNodes[name] = parent.appendChild(style);
  15. }
  16. }
  17. export default new DynamicCss();