首页 > Web开发 > 详细

jQuery插件——自定义jQuery插件

时间:2019-05-21 20:40:09      阅读:128      评论:0      收藏:0      [点我收藏+]

扩展插件

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
            })
        }
        
    })
})()

技术分享图片

 

jQuery插件——自定义jQuery插件

原文:https://www.cnblogs.com/yangHS/p/10901890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!