jQuery插件的开发包括两种:一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数;另一种是对象级别的插件开发,即给jQuery对象添加方法。本文提供的jQuery插件开发框架综合 ...
jQuery插件的开发包括两种:
一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数;另一种是对象级别的插件开发,即给jQuery对象添加方法。
本文提供的jQuery插件开发框架综合上述两种方式,代码如下所示:
// AMD support(function(factory) { //"use strict"; if (typeof define === 'function' && define.amd) { // using AMD; register as anon module define(['jquery'], factory); } else { // no AMD; invoke directly factory((typeof (jQuery) != 'undefined') ? jQuery : window.Zepto); }}(function($) { "use strict"; var pluginNS = 'InputBox', pluginPfx = 'IP', /* ---------------------------------------- VARS, CONSTANTS ---------------------------------------- */ totalInstances = 0, /* plugin instances amount */ /* ---------------------------------------- DEFAULT OPTIONS 默认参数 ---------------------------------------- */ defaults = { nParam: 0 }, /* ---------------------------------------- METHODS 公有函数 ---------------------------------------- */ methods = { init: function(options) { var opts = $.extend(true, {}, defaults, options); /* plugin constructor */ return this.each(function() { $this = $(this); if (!$this.data(pluginPfx)) { /* prevent multiple instantiations */ /* store options and create objects in jquery data */ $this.data(pluginPfx, { idx: ++totalInstances, /* instance index */ opt: opts /* options */ });
原标题:jQuery插件的开发
关键词:jquery
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。