jQuery插件——自定义jQuery插件

扩展插件

1.扩展jQuery的工具方法

  $.extend(object)

2.扩展jQuery对象的方法

  $.fn.extend(object)

(function () {
    /**
     * 需求:
     * 1.给$添加4个工具方法
     *  min(a,b) : 返回较小的值
     *  max(c,d) : 返回较大的值
     *  leftTrim() : 去掉字符串左边的空格
     *  rightTrim() : 去掉字符串右边的空格
     *
     *  2.给jQuery对象 添加3个功能方法:
     *  checkAll() : 全选
     *  unCheckAll() : 全不选
     *  reverseCheck() : 全反选
     */
    //扩展$的方法
    $.extend({
        min:function (a,b) {
          return a > b ? b : a
        },
        max:function (a,b) {
            return a < b ? b : a
        },
        leftTrim:function (str) {
            return str.replace(/^\s+/,'')
        },
        rightTrim:function (str) {
            return str.replace(/\s+$/,'')
        },
    })
    //扩展jQuery对象的方法
    $.fn.extend({
        checkAll:function () {
            this.prop('checked',true)//this是jQuery对象
        },
        unCheckAll:function () {
            this.prop('checked',false)
        },
        reverseCheck:function () {
            //this是jQuery对象
            this.each(function () {
                //this是dom元素
                this.checked = !this.checked
            })
        }
        
    })
})()

 

posted @ 2019-05-21 19:37  鸿森  阅读(88)  评论(0编辑  收藏  举报