扩展JQUERY内部API:两种方法
jQuery.extend({
siren:function(){
alert(123)
}
})
$.extend({
siren:function(){
alert(333)
}
})
调用方法是 jQuery.siren();
如果针对组件的功能扩展函数: 重载某方法
$.fn.hightlight
= function(colorName) {
this.mouseover(function() {
$(this).css('background-color',
colorName); //this对是对组件自身的引用
});
this.mouseout(function() {
$(this).css('background-color', '');
});
}
调用方法 $(“#obj”). hightlight();
另外一种就是
(function($){
$.fn.extend({
siren:function(){
alert(123)
}
})
})(jQuery);
调用方法
$(“#siren”).siren();
本文介绍了如何通过两种方式扩展jQuery的方法:一是直接使用jQuery.extend方法添加全局功能;二是利用$.fn.extend来为jQuery对象添加方法。此外,还展示了如何重载jQuery方法以实现组件级别的功能扩展。
718

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



