jQuery.extend
jQuery.extend可以扩展jQuery对象本身。
用来在jQuery命名空间上增加新函数,或者合并对象。
1. 增加方法:
$.extend({
hello: function () {
console.log(this)//function jQuery(selector, context)
}
});
$.hello();
- 合并对象
var obj1 = {'name' : 'sheng00'};
var obj2 = {'sex' : 'Male'};
$.extend(obj1, obj2);
console.log(obj1)//Object {name: "sheng00", sex: "Male"}
jQuery.fn.extend
用来扩展 jQuery 元素集来提供新的方法(通常用来制作插件)
$.fn.extend({
turn_red: function () {
return this.each(function () {
this.style.color = 'red'
});
}
});
$('.elements').turn_red(); // sets color to red
效果和jQuery.fn.turn_red=function(){}
是一样的