$.format = function (source, params) {
///<summary>字符串格式化调用方法 <br /> var text = "a{0}b{0}c{1}d\nqq{0}"; <br /> var text2 = $.format(text, 1, 2); </summary>
///<param name="source" type="string">要格式化的字符串</param>
///<param name="params" type="动态值">要填充的值</param>
if (arguments.length == 1)
return function () {
var args = $.makeArray(arguments);
args.unshift(source);
return $.format.apply(this, args);
};
if (arguments.length > 2 && params.constructor != Array) {
params = $.makeArray(arguments).slice(1);
};
if (params.constructor != Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
});
return source;
};
$.HTMLDecode = function(text) {
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
};
$.getScriptFolder = function() {
///<summary>获取当前js文件的路径</summary>
var js = document.scripts;
return js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf("/") + 1);
};
$.genId = function() {
///<summary>模拟一串guid的数字</summary>
var S4 = function() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
};
$.addTips = function(obj, message) {
///<summary>对控件添加提示</summary>
///<param name="obj" type="dom对象">要添加提示的对象</param>
///<param name="message" type="string">提示的信息</param>
$(obj).addClass("error");
$(obj).attr("title", message);
$(obj).tooltip({
position: "right",
onShow: function() {
$(this).tooltip('tip').css({
'background-color': '#f9f2ba',
'border': '1px solid #e9d315',
'color': '#5b5316',
'padding': '5px'
});
}
});
};
$.removeTips = function(obj) {
///<summary>移除控件提示</summary>
///<param name="obj" type="dom对象">要移除提示的对象</param>
if ($(obj).attr("class") && $(obj).attr("class").indexOf("error") > -1) {
$(obj).tooltip("destroy");
}
$(obj).removeClass("error");
$(obj).removeAttr("title");
};
//日期格式化
//调用 var d = new Date().format('yyyy-MM-dd');
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
};
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
return format;
};
//获取字符串的字节数
$.getByteLen = function(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match( /[^\x00-\xff]/ig ) != null) {
len += 2;
} else {
len += 1;
}
}
return len;
};
$.getByteVal = function (val, len, tips) {
var reg = /[^\x00-\xff]/g, mid = Math.floor(len / 2), tips = tips || "...";
if (val.replace(reg, "mm").length <= len) return val;
for (var i = mid; i < val.length; i++) {
if (val.substr(0, i).replace(reg, "mm").length >= len) {
return val.substr(0, i) + tips;
}
}
return val;
};
$.arrUnique = function (arr) {
if (!arr || arr.constructor!==Array) return arr;
var tempA = [], tempO = {};
for (var i = 0, len = arr.length; i < len; i++) if (!tempO[arr[i]]) tempO[arr[i]] = 1;
for (var j in tempO) tempA.push(j);
return tempA;
};
//取url参数的值
$.getUrlParameter = function(key) {
var par = location.search;
var strReg = "[&|\\?]" + key + "=([^\&]*)";
var reg = new RegExp(strReg, "i");
if (reg.test(par)) {
return RegExp.$1;
}
return "";
};
///<summary>字符串格式化调用方法 <br /> var text = "a{0}b{0}c{1}d\nqq{0}"; <br /> var text2 = $.format(text, 1, 2); </summary>
///<param name="source" type="string">要格式化的字符串</param>
///<param name="params" type="动态值">要填充的值</param>
if (arguments.length == 1)
return function () {
var args = $.makeArray(arguments);
args.unshift(source);
return $.format.apply(this, args);
};
if (arguments.length > 2 && params.constructor != Array) {
params = $.makeArray(arguments).slice(1);
};
if (params.constructor != Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
});
return source;
};
$.HTMLDecode = function(text) {
var temp = document.createElement("div");
temp.innerHTML = text;
var output = temp.innerText || temp.textContent;
temp = null;
return output;
};
$.getScriptFolder = function() {
///<summary>获取当前js文件的路径</summary>
var js = document.scripts;
return js[js.length - 1].src.substring(0, js[js.length - 1].src.lastIndexOf("/") + 1);
};
$.genId = function() {
///<summary>模拟一串guid的数字</summary>
var S4 = function() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
};
return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
};
$.addTips = function(obj, message) {
///<summary>对控件添加提示</summary>
///<param name="obj" type="dom对象">要添加提示的对象</param>
///<param name="message" type="string">提示的信息</param>
$(obj).addClass("error");
$(obj).attr("title", message);
$(obj).tooltip({
position: "right",
onShow: function() {
$(this).tooltip('tip').css({
'background-color': '#f9f2ba',
'border': '1px solid #e9d315',
'color': '#5b5316',
'padding': '5px'
});
}
});
};
$.removeTips = function(obj) {
///<summary>移除控件提示</summary>
///<param name="obj" type="dom对象">要移除提示的对象</param>
if ($(obj).attr("class") && $(obj).attr("class").indexOf("error") > -1) {
$(obj).tooltip("destroy");
}
$(obj).removeClass("error");
$(obj).removeAttr("title");
};
//日期格式化
//调用 var d = new Date().format('yyyy-MM-dd');
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, //month
"d+": this.getDate(), //day
"h+": this.getHours(), //hour
"m+": this.getMinutes(), //minute
"s+": this.getSeconds(), //second
"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
"S": this.getMilliseconds() //millisecond
};
if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
(this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o) if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
return format;
};
//获取字符串的字节数
$.getByteLen = function(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
if (val[i].match( /[^\x00-\xff]/ig ) != null) {
len += 2;
} else {
len += 1;
}
}
return len;
};
$.getByteVal = function (val, len, tips) {
var reg = /[^\x00-\xff]/g, mid = Math.floor(len / 2), tips = tips || "...";
if (val.replace(reg, "mm").length <= len) return val;
for (var i = mid; i < val.length; i++) {
if (val.substr(0, i).replace(reg, "mm").length >= len) {
return val.substr(0, i) + tips;
}
}
return val;
};
$.arrUnique = function (arr) {
if (!arr || arr.constructor!==Array) return arr;
var tempA = [], tempO = {};
for (var i = 0, len = arr.length; i < len; i++) if (!tempO[arr[i]]) tempO[arr[i]] = 1;
for (var j in tempO) tempA.push(j);
return tempA;
};
//取url参数的值
$.getUrlParameter = function(key) {
var par = location.search;
var strReg = "[&|\\?]" + key + "=([^\&]*)";
var reg = new RegExp(strReg, "i");
if (reg.test(par)) {
return RegExp.$1;
}
return "";
};