$.extend(prop) 扩展jQuery对象。可以用于把函数添加到jQuery名称空间中,以及添加插件方法(插件)。 返回值:Object参数:prop (Object): 要合并到jQuery对象中的对象
1, 添加方法,如同$("button").click()方法一样使用:
jQuery.fn.extend({
check: function() {
return this.each(function() { this.checked = true; });
},
uncheck: function() {
return this.each(function() { this.checked = false; });
}
});
然后直接调用方法
选中所有checkbox ; $(":checkbox").check();
取消选中radio:$(":radio").uncheck();
2, 为jQuery对象添加函数 如同$.trim()一样使用:
jQuery.extend({
min: function(a, b) { return a < b?? a?: b; },
max: function(a, b) { return a > b?? a?: b; }
});
可以直接调用函数如: $.min(5,4).
本文介绍了如何使用jQuery.fn.extend()扩展jQuery对象的方法与函数,包括添加方法和为jQuery对象添加自定义函数,并展示了实际应用案例。
978

被折叠的 条评论
为什么被折叠?



