jquery中的$.fn的用法

一、$.fn.method()=function(){}和$.fn.extend({})的比较

jQuery.fn === jQuery.prototype

1.$.fn.method()=function(){}的调用把方法扩展到了对象的prototype上,所以实例化一个jQuery对象的时候,它就具有了这些方法。

$.fn.myExtension = function(){
 var currentjQueryObject = this;
 //work with currentObject
 return this;//you can include this if you would like to support chaining
};

$.fn.blueBorder = function(){
 this.each(function(){
  $(this).css("border","solid blue 2px");
 });
 return this;
};
$.fn.blueText = function(){
 this.each(function(){
  $(this).css("color","blue");
 });
 return this;
};

由于有return this,所以支持链式,在调用的时候可以这样写:$('.blue').blueBorder().blueText();

2.$.fn.extend({}) 是对$.fn.method()=function(){}的扩展,它可以定义多个方法:

$.fn.extend({
    a: function() { },
    b: function() { }
});
$.fn.a = function() { };
$.fn.b = function() { };

二、$.extend({})  ,为jQuery类添加方法,可以理解为扩展静态方法

$.extend({
    abc: function(){
        alert('abc');
    }
});

usage: $.abc(). (No selector required like $.ajax().)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值