该示例用以在JQuery中增加新函数,该函数是静态函数。
$.extend({
max: function(a, b) {
return a > b ? a : b;
},
min: function(a, b) {
return a > b ? b : a;
},
avg: function(a, b) {
return a / b;
}
});
调用如下:
jQuery.min(2,3); // => 2
jQuery.max(4,5); // => 5
如果针对组件的功能扩展函数:
$.fn.hightlight = function(colorName) {
this.mouseover(function() {
$(this).css(‘background-color’, colorName); //this对是对组件自身的引用
});
this.mouseout(function() {
$(this).css(‘background-color’, ”);
});
}
调用如下:
$(function() {
$(‘#test’).hightlight(‘red’);
});
本文属于转载+修改
$.fn.hightlight = function(colorName) {
return this.hover(function() { //加入return可以串联后续API,hover用了跨浏览器的mouseenter/mouseleave
$(this).css(‘background-color’, colorName);
},function() {
$(this).css(‘background-color’, ”);
});
}
fantastic post, very informative. I wonder why the other experts of this sector don’t notice this. You should continue your writing. I am sure, you have a huge readers’ base already!