javascript插件编写小结

      写JS插件,最好是先通过HTML方式将展示结果显示出来,然后再封装成JS插件,将其画出来。JS模板如下:

(function($){
   $.fn.fnName = function(options){
      var opts = $.extend({},$.fn.fnName.Defaults,options);
      return this.each(function(){
          var $window;
          CreateTemplate(this); // 创建模板,也就是拼接一个界面
          SetStyleForTemplate(); // 给模板添加样式
          SetEventForTemplate(); //添加事件

	  function CreateTemplate(w){
             $window = $(w);
             var arrcontent = [];
             arrcontent.push("*********");
             *************************** //拼接
             CreateSubTemplate(arrcontent);//分支拼接,特殊情况利于层次分明

             $window.html(arrcontent.join(""));
             arrcontent = null;
          }

	  functon CreateSubTemplate(arrcontent){
	     arrcontent.push("*********");
          }

	  function SetStyleForTemplate(){
             $window.find("***")*************;//设置样式
          }

          function SetEventForTemplate(){
             $window.find("***")************;//添加事件
          }
      });
   };
   $.fn.fnName.Defaults ={
   // 默认参数
   };
})(jQuery);

  

原文地址:https://www.cnblogs.com/kinger906/p/3526263.html