如何扩展JQUERY API

扩展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,function,api,扩展)