js 独立创建样式模块

;!function (window,undefined) {
   var aa = {
      alert:function(msg){
         var html = '<p>hello world</p>';
         window.document.body.appendHTML(html);
         console.log(msg);
      }
   }

   HTMLElement.prototype.appendHTML = function(html) {
      var divTemp = document.createElement("div"), nodes = null
          // 文档片段,一次性append,提高性能
          , fragment = document.createDocumentFragment();
      divTemp.innerHTML = html;
      nodes = divTemp.childNodes;
      for (var i=0, length=nodes.length; i<length; i+=1) {
         fragment.appendChild(nodes[i].cloneNode(true));
      }
      this.appendChild(fragment);
      // 据说下面这样子世界会更清净
      nodes = null;
      fragment = null;
   };

   window.aa = aa;
}(window);

猜你喜欢

转载自blog.csdn.net/nanshan_hzq/article/details/109112317
今日推荐