如何编写一个Jquery插件
跟我一起学JQuery插件开发http://www.cnblogs.com/damonlan/archive/2012/04/06/2434460.html
jQuery插件开发全解析http://www.iteye.com/topic/545971
jQuery插件开发的模式和结构 http://www.cnblogs.com/cyStyle/archive/2013/05/18/jQuery%E6%8F%92%E4%BB%B6%E8%AF%A6%E7%BB%86%E5%BC%80%E5%8F%91.html
jQuery插件的开发包括两种:
一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法。jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级别的插件开发,即给jQuery对象添加方法。下面就两种函数的开发做详细的说明。
1、类级别的插件开发
类级别的插件开发最直接的理解就是给jQuery类添加类方法,可以理解为添加静态方法。典型的例子就是$.AJAX()这个函数,将函数定义于jQuery的命名空间中。关于类级别的插件开发可以采用如下几种形式进行扩展:
1.1 添加一个新的全局函数
添加一个全局函数,我们只需如下定义:
jQuery.foo = function() { alert('This is a test. This is only a test.'); };
jQuery.foo = function() { alert('This is a test. This is only a test.'); }; jQuery.bar = function(param) { alert('This function takes a parameter, which is "' + param + '".'); };
jQuery.extend({ foo: function() { alert('This is a test. This is only a test.'); }, bar: function(param) { alert('This function takes a parameter, which is "' + param +'".'); } });
jQuery.myPlugin = { foo:function() { alert('This is a test. This is only a test.'); }, bar:function(param) { alert('This function takes a parameter, which is "' + param + '".'); } };
$.myPlugin.foo(); $.myPlugin.bar('baz');
(function($){ $.fn.extend({ pluginName:function(opt,callback){ // Our plugin implementation code goes here. } }) })(jQuery);
(function($) { $.fn.pluginName = function() { // Our plugin implementation code goes here. }; })(jQuery);
$.fn.hilight = function() { // Our plugin implementation code goes here. };
$('#myDiv').hilight();
// plugin definition $.fn.hilight = function(options) { var defaults = { foreground: 'red', background: 'yellow' }; // Extend our default options with those provided. var opts = $.extend(defaults, options); // Our plugin implementation code goes here. };
$('#myDiv').hilight({ foreground: 'blue' });
// plugin definition $.fn.hilight = function(options) { // Extend our default options with those provided. // Note that the first arg to extend is an empty object - // this is to keep from overriding our "defaults" object. var opts = $.extend({}, $.fn.hilight.defaults, options); // Our plugin implementation code goes here. }; // plugin defaults - added as a property on our plugin function $.fn.hilight.defaults = { foreground: 'red', background: 'yellow' };
// 覆盖插件缺省的背景颜色 $.fn.hilight.defaults.foreground = 'blue'; // ... // 使用一个新的缺省设置调用插件 $('.hilightDiv').hilight(); // ... // 通过传递配置参数给插件方法来覆盖缺省设置 $('#green').hilight({ foreground: 'green' });
// plugin definition $.fn.hilight = function(options) { // iterate and reformat each matched element return this.each(function() { var $this = $(this); // ... var markup = $this.html(); // call our format function markup = $.fn.hilight.format(markup); $this.html(markup); }); }; // define our format function $.fn.hilight.format = function(txt) { return '<strong>' + txt + '</strong>'; };
(function($) { // plugin definition $.fn.hilight = function(options) { debug(this); // ... }; // private function for debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); }; // ... })(jQuery);
$.fn.hilight = function(options) { // ... // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); return this.each(function() { var $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; //...
<!-- markup --> <div name="code">// 创建一个闭包 (function($) { // 插件的定义 $.fn.hilight = function(options) { debug(this); // build main options before element iteration var opts = $.extend({}, $.fn.hilight.defaults, options); // iterate and reformat each matched element return this.each(function() { $this = $(this); // build element specific options var o = $.meta ? $.extend({}, opts, $this.data()) : opts; // update element styles $this.css({ backgroundColor: o.background, color: o.foreground }); var markup = $this.html(); // call our format function markup = $.fn.hilight.format(markup); $this.html(markup); }); }; // 私有函数:debugging function debug($obj) { if (window.console && window.console.log) window.console.log('hilight selection count: ' + $obj.size()); }; // 定义暴露format函数 $.fn.hilight.format = function(txt) { return '<strong>' + txt + '</strong>'; }; // 插件的defaults $.fn.hilight.defaults = { foreground: 'red', background: 'yellow' }; // 闭包结束 })(jQuery);
jQuery.fn = jQuery.prototype = { init: function( selector, context ) {//.... //...... };
$.fn.extend({ alertWhileClick:function(){ $(this).click(function(){ alert($(this).val()); }); } });
$.extend({ add:function(a,b){return a+b;} });
(function ($) { $.fn.hoverElement = function () { return this.each(function () { $(this).hover(function () { $(this).addClass("Add");},function () { $(this).removeClass("Remove");}); }) }})(jQuery);
$(document).ready(function () { $(".hoverText").hoverElement().css("color","yellow"); });