zoukankan      html  css  js  c++  java
  • js动态添加CSS

    (function() {
          var isLoaded = false;
          AddStyle = function(rules) {
            var styleElement = document.createElement('style');
            styleElement.type = 'text/css';
            if ( $IE ) {   //判断IE浏览器
              styleElement.styleSheet.cssText = rules;
            }
            else {
              var frag = document.createDocumentFragment();
              frag.appendChild(document.createTextNode(rules));
              styleElement.appendChild(frag);
            }
            function append() {
              document.getElementsByTagName('head')[0].appendChild(styleElement);
            }
    		//IE 无法取到document
            if (!$IE || isLoaded) { 
              append();
            }
            else {
              window.attachEvent('onload', function() {
                isLoaded = true;
                append();
              });
            }
          };
    	  })();
    

    用法:

    AddStyle('body { color: #CCC; }')
    
  • 相关阅读:
    购物车宣传页
    项目开发流程
    AJAX跨域
    jQuery中的AJAX
    AJAX封装
    AJAX里使用模板引擎
    AJAX的具体使用
    AJAX的基本使用
    js技巧汇总
    CSS特效汇集
  • 原文地址:https://www.cnblogs.com/feng_013/p/1807520.html
Copyright © 2011-2022 走看看