jquery.fn.extend() 与 $.jquery 作用及区别

时间:2023-03-10 02:33:31
jquery.fn.extend() 与 $.jquery 作用及区别

原文:http://www.cnblogs.com/liu-l/p/3928373.html

jQuery.extend()这个方法,主要是用来拓展个全局函数啦,例如$.ajax()这种,要不就是拓展个选择器啦,例如$.fn.each(),当选择器用。

jQuery.fn.extend拓展的是jQuery对象(原型的)的方法啊!那就是说,jQuery.fn.extend拓展的方法,你得用在jQuery对象上面才行啊!

大部分插件都是用jQuery.fn.extend()。‘

;(function(){
jQuery.fn.extend({
"color":function(value){
return this.css("color",value);
}
});
$.extend({
ltrim:function( text ){
return (text || "").replace(/^\s+/g,"");
},
rtrim:function( text ){
return (text || "").replace(/\s+$/g,"");
}
});
})(jQuery);

$('#color').on('change',function(){
   $('.s').color($(this).val());
});
var $str = " test ";
console.log($.trim($str));//'test'
console.log($.ltrim($str));//'test '
console.log($.rtrim($str));//' test'