博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
给jquery对象添加自定义方法和扩展jquery类
阅读量:5133 次
发布时间:2019-06-13

本文共 6074 字,大约阅读时间需要 20 分钟。

http://blog.sina.com.cn/s/blog_944b24ef0101epr5.html

 
一、给jQuery对象添加自定义方法
 
    方法一、$.fn.
xxx
 
    方法二、jQuery.fn.extend({
        
xxx:function(){
            alert($(this).val());
        }
    });
 
    方法一示例:
 
    $.fn.setCursorPosition = function(position){
            if(this.lengh == 0) return this;
            return $(this).setSelection(position, position);
    }
       
    $.fn.setSelection = function(selectionStart, selectionEnd) {
            if(this.lengh == 0) return this;
            input = this[0];
       
            if (input.createTextRange) {
                    var range = input.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', selectionEnd);
                    range.moveStart('character', selectionStart);
                    range.select();
            } else if (input.setSelectionRange) {
                    input.focus();
                    input.setSelectionRange(selectionStart, selectionEnd);
            }
       
            return this;
    }
       
    $.fn.focusEnd = function(){
            this.setCursorPosition(this.val().length);
    }
 
    
    方法二示例:
 
    $.fn.extend({
            setCursorPosition:function(position){
                    if(this.lengh == 0) return this;
                    return $(this).setSelection(position, position);
            },
            setSelection:function(selectionStart, selectionEnd) {
                    if(this.lengh == 0) return this;
                    input = this[0];
               
                    if (input.createTextRange) {
                            var range = input.createTextRange();
                            range.collapse(true);
                            range.moveEnd('character', selectionEnd);
                            range.moveStart('character', selectionStart);
                            range.select();
                    } else if (input.setSelectionRange) {
                            input.focus();
                            input.setSelectionRange(selectionStart, selectionEnd);
                    }
               
                    return this;
            },
            focusEnd:function(){
                    this.setCursorPosition(this.val().length);
            }
    });
 
   
    以上定义都可如此调用:
 
    $('.num').click(function(){
            $('.num').focusEnd();
    });
 
   
二、扩展jQuery类本身  为类添加新的方法
 
    方法一、jQuery.extend(object);
 
    方法二、jQuery.
xxx=function(){};
    
    方法一示例:
 
    $.extend({
        add:function(a,b){return a+b;},
        a:'2 chi gege '
    });
    alert($.add(3,4)); //7
    alert($.a); //2 chi gege
 
    方法二示例:
 
    $.add2 = function(a,b){
            return a-b;
    }
    alert($.add2(4,3)); //1
 
 
   参考代码:

$.hx = {};
$.hx.data = {
isEmpty: function (value) {
var item = $.trim(value);
return item.length === 0;
},
isNumber: function (value) {
return !isNaN(item);
}
};

$.hx.regex = {

expression: {
email: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/, //e.g. admin@pailv.com
password: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{7,20}$/,//e.g. admin888
cabin: /^[a-zA-Z]{1}$/,
cabins: /^([a-zA-Z]{1}\/){1,30}$/,
airline: /^(([A-Za-z]{2})|([A-Za-z]\d)|(\d[A-Za-z])){1}$/,
mainAirportCode: /^([a-zA-Z]{3}\/){1,10}$/,//e.g. pvg/PVG/pVg/
airportCode: /^([a-zA-Z]{3}\/){1,300}$/,//e.g. pvg/PVG/pVg/ 最大改成了300个机场,原先的一些最大数在控件中使用maxlength做了限制 update by wss on20150511
unsignedInteger: /^\d+$/,
integer: /^(-){0,1}\d+$/,
flightNo: /^((\d{1,4}-\d{1,4}\/)|(\d{1,4}\/)){0,10}$/, //e.g. 1/2/6666-8888/9000-9999/
date: /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/,
currencyCode: /^[a-zA-Z]{3}$/,
double: /^(-)?\d+(\.\d+)?$/,
decimal: /^((\d|[1-9]\d+)(\.\d{1,2}){0,1}){1}$/,
cityCode: /^[A-Za-z]{3}(\/{1}[A-Za-z]{3}){0,}?$/,
invoiceAirline: /^[0-9A-Za-z]{2}(\/[0-9A-Za-z]{2}){0,}?$/,
CabinLevel: /^[A-Za-z]{1}(\/[A-Za-z]{1}){0,}?$/,
Airport: /^[a-zA-Z]{3}$/,
airlines: /^((([A-Za-z]{2})|([A-Za-z]\d)|(\d[A-Za-z]))\/){1,100}$/

},

matchReg: function (source, reg) {
source.match(reg);
},
matchEmail: function (source) {
return source.match(this.expression.email);
},
matchPassword: function (source) {
return source.match(this.expression.password);
},
matchCabin: function (source) {
return source.match(this.expression.cabin);
},
matchCabins: function (source, autoAppend) {
var result = source;
if (autoAppend && result.lastIndexOf("/") != result.length - 1) {
result = result + "/";
}
return result.match(this.expression.cabins);
},
matchAirline: function (source) {
return source.match(this.expression.airline);
},
matchMainAirportCode: function (source, autoAppend) {
var result = source;
if (autoAppend && result.lastIndexOf("/") != result.length - 1) {
result = result + "/";
}
return result.match(this.expression.mainAirportCode);
},
matchAirportCode: function (source, autoAppend) {
var result = source;
if (autoAppend && result.lastIndexOf("/") != result.length - 1) {
result = result + "/";
}
return result.match(this.expression.airportCode);
},
matchUnsignedInteger: function (source) {
return source.match(this.expression.unsignedInteger);
},
matchFlightNo: function (source, autoAppend) {
var result = source;
if (autoAppend && result.lastIndexOf("/") != result.length - 1) {
result = result + "/";
}
return result.match(this.expression.flightNo);
},
matchDate: function (source) {
return source.match(this.expression.date);
},
matchInteger: function (source) {
return source.match(this.expression.integer);
},
matchIntegerRange: function (source, min, max) {
if (!source.match(this.expression.integer)) {
return false;
}
if (Number(source) < min || Number(source) > max) {
return false;
}
return true;
},
matchCurrencyCode: function (source) {
return source.match(this.expression.currencyCode);
},
matchDouble: function (source) {
return source.match(this.expression.double);
},
matchDecimal: function (source) {
return source.match(this.expression.decimal);
},
matchCityCode: function (source) {
return source.match(this.expression.cityCode);
},
matchInvoiceAirline: function (source) {
return source.match(this.expression.invoiceAirline);
},
matchCabinLevel: function (source) {
return source.match(this.expression.CabinLevel);
},
matchAirport: function (source) {
return source.match(this.expression.Airport);
},
matchAirlines: function (source) {
var result = source;
if (result.lastIndexOf("/") != result.length - 1) {
result = result + "/";
}
return result.match(this.expression.airlines);
}
};

$.hx.style = {

addError: function (o) {
o.addClass("error");
o.focus();
},
removeError: function (o) {
o.removeClass("error");
},
addSubmitting: function (o) {

}

};

$.hx.compare = {
date: function (start, end) {
var startTime = new Date(start.replace(/-/gi, "/"));
var endTime = new Date(end.replace(/-/gi, "/"));
if (endTime < startTime) {
return false;
}
return true;
}
};

转载于:https://www.cnblogs.com/xiaocandou/p/4560377.html

你可能感兴趣的文章
期中总结
查看>>
冲刺四
查看>>
谷歌技术"三宝"之BigTable(转)
查看>>
第五次作业
查看>>
[leetcode-117]填充每个节点的下一个右侧节点指针 II
查看>>
洛谷 P2622 关灯问题II (状态压缩+BFS)
查看>>
LeetCode Jump Game
查看>>
centos6下安装git
查看>>
#C++初学记录(算法测试2019/5/5)(深度搜索)
查看>>
数据从mysql迁移至oracle时知识点记录(一)
查看>>
[No0000120]Python教程3/9-第一个Python程序
查看>>
java-web乱码问题解决
查看>>
222 Count Complete Tree Nodes
查看>>
SpringMVC拦截器
查看>>
Java Database Derby Instance 2
查看>>
[Django]我的第一个网页,报错啦~(自己实现过程中遇到问题以及解决办法)
查看>>
python-笔记(二)数据类型
查看>>
VS2013的一些常用快捷键
查看>>
如何去各型MCU的官网上下载正确的数据手册
查看>>
Ulink2 "No Ulink Device found" 解决办法
查看>>