jQuery为开发插件提拱了两个方法,分别是:

jQuery.extend(object)

 jQuery.fn.extend(object)

 

jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。可以理解为添加静态方法

示例如下,返回两个数种较大的一个

$.extend({

     Max:function(a,b){

          if(a>b){

             return a;

          }else{

             return b;

           }

         }

});

调用方法:

var max=$.Max(10,100);//返回两个数种较大的一个

 

jQuery.fn.extend(object);给jQuery对象添加方法,对jQuery.prototype进行扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。

 

查看fn的jQuery代码如下:

jQuery.fn=jQuery.prototype={

   init:function(select,context){}

};

发现jQuery.fn = jQuery.prototype,是对其提供扩展方法,

下面使用jQuery.fn开发一个小插件,但文本框获取焦点以后清空文本框的内容

jQuery.fn.extend({

    cleartext:function(){

          $(this).focus(function(){

         $(this).val("");

});

}

});

调用方法如下:

$(document).ready(function(){

     $("input[type='text']").cleartext();

});

<html>

  <head>

     <title></title>

</head>

<body>

<input type="text" value="input username" />

</body>

</html>