var commonfn = {};
!function (c) {
//删除字符串前后的空格
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//获取url地址栏参数值
function queryString(name) {
/// 获取QueryString
if (name == null || name == "" || name == undefined) {
var AllVars = window.location.search.substring(1);
return AllVars.split("&");
}
else {
var AllVars = window.location.search.substring(1);
var Vars = AllVars.split("&");
for (i = 0; i < Vars.length; i++) {
var Var = Vars[i].split("=");
if (Var[0] == name) return Var[1];
}
return "";
}
};
//改变地址栏参数值
function changeParam(name, value) {
var url = window.location.href;
var newUrl = "";
var reg = new RegExp("(^|)" + name + "=([^&]*)(|$)");
var tmp = name + "=" + value;
if (url.match(reg) != null) {
newUrl = url.replace(eval(reg), tmp);
}
else {
if (url.match("[\?]")) {
newUrl = url + "&" + tmp;
}
else {
newUrl = url + "?" + tmp;
}
}
location.href = newUrl;
}
//只能输入金额
function clearNoNum(obj) {
if (obj.value == "0") {
} else {
var firstNum = obj.value.toString().substring(0, 1);
if (obj.value.length > 1 && firstNum == "0") {
var second = obj.value.toString().substring(1, 2);
if (second == "0") {
obj.value = "0";
}
}
}
//先把非数字的都替换掉,除了数字和.
obj.value = obj.value.replace(/[^\d.]/g, "");
//必须保证第一个为数字而不是.
obj.value = obj.value.replace(/^\./g, "");
//保证只有出现一个.而没有多个.
obj.value = obj.value.replace(/\.{2,}/g, ".");
//保证.只出现一次,而不能出现两次以上
obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
}
//删除小数后面的小数点
function delNumHouDian(obj) {
var endstr = obj.value.substring(obj.value.length - 1);
if (endstr == ".") {
obj.value = obj.value.substring(0, obj.value.length - 1);
}
}
//小数格式化,num是要格式话的数字,len是要保留的小数位数
function cutXiaoNum(num, len) {
var numStr = num.toString();
if (len == null || len == undefined) {
len = numStr.length;
}
var index = numStr.indexOf('.');
if (index == -1) {
index = numStr.length;
numStr += ".0000000000000";
} else {
numStr += "0000000000000";
}
var newNum = numStr.substring(0, index + len + 1);
return newNum;
}
//将时间戳转换为时间字符串格式15:18:13
function getTimeStampTime(timeStamp) {
///<summary>
///将时间戳转换为时间字符串,返回格式15:18:13
/// </summary>
/// <param name="timeStamp">时间戳,整数</param>
var d = new Date(parseInt(timeStamp) * 1000);
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
if (d.getHours() == 0) {
h = "00";
}
if (d.getHours() < 10) {
h = "0" + d.getHours();
}
if (m == 0) {
m = "00";
}
if (d.getMinutes() < 10) {
m = "0" + d.getMinutes();
}
if (s == 0) {
s = "00";
}
if (d.getSeconds() < 10) {
s = "0" + d.getSeconds();
}
var time = h + ":" + m + ":" + s;
return time;
}
//将时间戳转换为分钟格式16:08
function getTimeStampToMinutes(timeStamp) {
///<summary>
///将时间戳转换为时间字符串,返回格式15:18
/// </summary>
/// <param name="timeStamp">时间戳,整数</param>
var d = new Date(parseInt(timeStamp));
var h = d.getHours();
var m = d.getMinutes();
if (d.getHours() == 0) {
h = "00";
}
if (d.getHours() < 10) {
h = "0" + d.getHours();
}
if (m == 0) {
m = "00";
}
if (d.getMinutes() < 10) {
m = "0" + d.getMinutes();
}
var time = h + ":" + m;
return time;
}
c.changeParam = changeParam;
c.queryString = queryString;
c.clearNoNum = clearNoNum;
c.delNumHouDian = delNumHouDian;
c.cutXiaoNum = cutXiaoNum;
c.getTimeStampTime = getTimeStampTime;
c.getTimeStampToMinutes = getTimeStampToMinutes;
}(commonfn);
js自己封装的开发使用工具方法总结
本文转载:CSDN博客