给jquery对象添加自定义方法和扩展jquery类

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. [email protected]
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;
}
};

你可能感兴趣的:(JQuery对象)