jquery的扩展之extend函数

1.$.extend()使用

作用:扩展全局的函数

$.extend({

sayHellow:function(pram){

  alert(pram+"hellow");

  }

})

使用:

$(function({

  $.sayHellow();

}))


jQuery.hellow = function() {   
alert('hellow ');  
};  
jQuery.byebye = function(param) {   
alert(param + 'byebye ');  
};   
使用:jQuery.hellow ();jQuery.byebye ('haha');
$.hellow ();$.byebye ('haha');
使用$.extend()扩展多个函数:
jQuery.extend({      
hellow: function() {alert('hellow ');  },
byebye: function(param) {alert(param + 'byebye ');} 
    });
使用命名空间:
第一种方法:
jQuery.myPlugin={  
    
    hellow: function() {alert('hellow ');  },

    byebye: function(param) {alert(param + 'byebye ');} 
    
    };
使用:
$.myPlugin.hellow();         
$.myPlugin.byebye('haha');

第二种方法:

$.extend({myPlugin:{}});

$.extend({$.myPlugin,{
      hellow: function() {alert('hellow ');  },

    byebye: function(param) {alert(param + 'byebye ');}
    
}})

 

 


 

2.$.fn.extend()使用

作用:扩展的是jquery的实例对象的函数(jquery.fn=jquery.prototype)

$.fn.extend({

  sayHellow:function(pram){

    alert(pram+"hellow");

    }

})

等于:

$.fn.sayHellow = function(pram) {     
     alert(pram+'hellow');  
};

使用:$(" ").sayHellow(pram);

附加:定义了一个jQuery函数,形参是$,函数定义完成之后,把jQuery这个实参传递进去.立即调用执行。这样的好处是,我们在写jQuery插件时,也可以使用$这个别名,而不会与prototype引起冲突.

(function($) {   
    
$.fn.pluginName = function() {     
   
};     
})(jQuery);   

3.接受参数options控制的插件

$.fn.pulginName = function(options) {
     var defaults = {
         property1: '',
         property2: '' }; 

 var opts = $.extend(defaults, options);
 
    };
调用:
 $('#div').hilight({
     property2: '' 
    });

  

 这是一个很不错的博客:http://www.iteye.com/topic/545971

转载于:https://www.cnblogs.com/cleverbo/p/5120550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值