jQuery插件机制


/*
 
 * 自定义jQuery插件,经过扩展JQuery对象自己进行扩展。
 * 一、能够经过一个当即执行函数 而后在使用$.extend({...}); 对$对象自己进行扩展
 * 调用时能够经过$.xxx(),$.xxx 调用到扩展方法及属性
 * 
 * 为了使得$被从新定义,出现冲突,致使没法使用,
 * 咱们在匿名函数中以$为形参,调用时使用jQuery做为实参.
 * 
 * 
 * */
(function($){
    $.extend({
        minValue:function(a,b){
            return a > b ? b : a;
        },
        MY_PI:3.141592654875641
    });
})(jQuery);dom

/*
 * 二、扩展jQuery对象.
 * 调用时须要先获取到jQuery对象,而后进行调用
 * $("xxx").yyy();
 * */
(function($){
    $.fn.extend({
        sayHello:function(){
            return "hello 你们好,我是一个插件方法";
        },
        //给jQuery的对象扩展全选功能,
        checkAll:function(){
            this.each(function(){  // 当前this是jQuery对象
                this.checked = true; // 此时的this是dom元素
            });
        },
        reverseCheck:function(){
            this.each(function(){  // 当前this是jQuery对象
                this.checked = !this.checked;
            });
        }
    });
})(jQuery);
 函数

相关文章
相关标签/搜索