Jquery的扩展


该示例用以在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’);
});

本文属于转载+修改

2 comments

  1. Cloudream 说道:

    $.fn.hightlight = function(colorName) {
    return this.hover(function() { //加入return可以串联后续API,hover用了跨浏览器的mouseenter/mouseleave
    $(this).css(‘background-color’, colorName);
    },function() {
    $(this).css(‘background-color’, ”);
    });
    }

  2. 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!

发表评论

电子邮件地址不会被公开。