jQuery.fn.extend(object)
扩展 jQuery 元素集来提供新的方法(通常用来制作插件)。
示例
增加两个插件方法。
jQuery 代码:
jQuery.fn.extend({
check: function() {
return this.each(function() { this.checked = true; });
},
uncheck: function() {
return this.each(function() { this.checked = false; });
}
});
check: function() {
return this.each(function() { this.checked = true; });
},
uncheck: function() {
return this.each(function() { this.checked = false; });
}
});
结果:
$("input[type=checkbox]").check();
$("input[type=radio]").uncheck();
$("input[type=radio]").uncheck();
jQuery.extend(object)
扩展jQuery对象本身。
用来在jQuery命名空间上增加新函数。
示例
在jQuery命名空间上增加两个函数。
jQuery 代码:
jQuery.extend({
min: function(a, b) { return a < b ? a : b; },
max: function(a, b) { return a > b ? a : b; }
});
min: function(a, b) { return a < b ? a : b; },
max: function(a, b) { return a > b ? a : b; }
});
结果:
jQuery.min(2,3); // => 2
jQuery.max(4,5); // => 5
jQuery.max(4,5); // => 5