ITPub博客

首页 > IT职业 > IT职场 > jquery中的$.fn的用法

jquery中的$.fn的用法

IT职场 作者:penny_liang 时间:2021-09-09 10:50:04 0 删除 编辑

jquery中的$.fn的用法

一、.fn.method()=function()和.fn.method()=function(){}和.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.extend({}) 是对.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().)(pos)


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/714/viewspace-2826532/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-12

  • 博文量
    189
  • 访问量
    1741