jQuery为开发插件提拱了两个方法,分别是:css
jQuery.fn.extend(object); jquery
jQuery.extend(object);this
jQuery.extend(object); 为扩展jQuery类自己.为类添加新的方法。spa
jQuery.fn.extend(object);给jQuery对象添加方法。prototype
fn 是什么东西呢。查看jQuery代码,就不难发现。插件
jQuery.fn = jQuery.prototype = { code
init: function( selector, context ) {//.... 对象
//...... blog
}; ip
原来 jQuery.fn = jQuery.prototype.
jQuery即是一个封装得很是好的类,好比咱们用 语句 $("#btn1") 会生成一个 jQuery类的实例。
jQuery.extend(object); 为jQuery类添加添加类方法,能够理解为添加静态方法。如:
$.extend({
add:function(a,b){return a+b;}
});
$.add(3,4); //return 7
jQuery.fn.extend(object); 对jQuery.prototype进行扩展
<script> // $.fn.changeColor=function(color){ // this.css('background',color); //this指向调用它的对象box // }; $.fn.extend({ cs:function(name,value){ this.css(name,value); //this是jquery对象 }, }); </script> <script> $(function(){ $('#box').cs('background','red'); $('#box1').cs('background','green'); $('#box1').cs('width','300px'); }); </script>