js常用函数的封装

喜你入骨 提交于 2019-12-29 00:41:25

1://截取url带过来的参数

function getvl(name) {    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");    return reg.test(location.href) ? decodeURI(RegExp.$2.replace(/\+/g, " ")) : '';};2://判断对象的长度
Object.size = function (obj) {    var size = 0, key;    for (key in obj) {        if (obj.hasOwnProperty(key)) size++;    }    return size;};3:// 转换字符串成时间function changeTime(t, nohour) {
    if (String(t).indexOf("-") >= 0 || t == null) {        return t = '';    } else {        var date = new Date(t);        Y = date.getFullYear() + '-';        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';        D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';        h = date.getHours() < 10 ? '0' + date.getHours() + ':' : date.getHours() + ':';        m = date.getMinutes() < 10 ? '0' + date.getMinutes() + ':' : date.getMinutes() + ':';        s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();        return nohour ? Y + M + D : Y + M + D + h + m + s;    }}
4://纯数字且不能输入空格且不能输入小数点// isZero == zero可为零// isZero == unNone不能为空格function keepNum(obj, num, isZero) {    var $this = $(obj);    var v = $this.val();    if (isNaN(v) || (v.indexOf('.') != -1)) {        var values = v.slice(0, -1);        if (isNaN(values)) {            values = ''        }        values = values.replace(/\s/g, "");        $this.val(values);    } else {  //去除空格        $this.val(v.replace(/\s/g, ""));    }    num && (function () {        var newValue = $this.val();        if (!isNaN(newValue) && newValue != '') {            if (isZero == 'zero') {                if (newValue < 0) {                    newValue = 0;                } else if (newValue > num) {                    newValue = num;                }            } else {                if (newValue < 1) {                    newValue = 1;                } else if (newValue > num) {                    newValue = num;                }            }        } else {            if (isZero == 'unNone') {                if (newValue < 1) {                    newValue = 1;                } else if (newValue > num) {                    newValue = num;                }            }        }        $this.val(newValue);    })()}5://纯数字且不能输入空格且不能输入小数点可以是负数   默认没有小数function keepSR(obj, num,maxlength) {    var imposeNum = maxlength ? (maxlength+1) : 1;    var $this = $(obj);    var v = $this.val();    if (isNaN(v)) {        if(v!='-'){            var values = v.slice(0, -1);            if (isNaN(values)) {                values = ''            }            values = values.replace(/\s/g, "");            $this.val(values);        }       } else if (v.indexOf('.') != -1) {        if (v.split('.')[1].length == imposeNum) {            var values = v.slice(0, -1);            $this.val(values);        } else if (v.split('.')[1].length > imposeNum) {            $this.val('');        }    }else {  //去除空格        $this.val(v.replace(/\s/g, ""));    }    num && (function () {        var newValue = $this.val();        if (!isNaN(newValue) && newValue != '') {            if (newValue < -num) {                newValue = -num;            } else if (newValue > num) {                newValue = num;            }        }        $this.val(newValue);    })()}//数字且最限制小数点位数,(默认三位)且不能输入空格且可以为0function keepSH(obj, num ,maxlength) {    var $this = $(obj);    var v = $this.val();    var imposeNum = maxlength ? (maxlength+1) : 4;//限制小数点数量,默认为小数点后3位    if (isNaN(v)) {        var values = v.slice(0, -1);        if (isNaN(values)) {            values = ''        }        $this.val(values);    } else if (v.indexOf('.') != -1) {        if (v.split('.')[1].length == imposeNum) {            var values = v.slice(0, -1);            $this.val(values);        } else if (v.split('.')[1].length > imposeNum) {            $this.val('');        }    } else {        $this.val(v.replace(/\s/g, ""));    }    var newValue = $this.val();    if (!isNaN(newValue) && newValue != '') {        if (newValue > num) {            newValue = num;        }    }    $this.val(newValue);}function keepSH2(obj, num ,maxlength) {    var $this = $(obj);    var v = $this.val();    var imposeNum = maxlength ? (maxlength+1) : 4;//限制小数点数量,默认为小数点后3位    if (isNaN(v)) {        var values = v.slice(0, -1);        if (isNaN(values)) {            values = ''        }        $this.val(values);    } else if (v.indexOf('.') != -1) {        if (v.split('.')[1].length == imposeNum) {            var values = v.slice(0, -1);            $this.val(values);        } else if (v.split('.')[1].length > imposeNum) {            $this.val('');        }    } else {        $this.val(v.replace(/\s|-/g, ""));    }    var newValue = $this.val();    if (!isNaN(newValue) && newValue != '') {        if (newValue > num) {            newValue = num;        }    }    $this.val(newValue);}
// 检查是否为手机号function checkMobile(text) {    var myreg = /^(1+\d{10})$/;    return !myreg.test(text) ? false : true;}//仅输入中文,字母,数字function unCharacter(v) {    return v.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5]/g,'')}//禁止特殊字符function unSpecial(v) {    return v.replace(/[^u4e00-u9fa5w]/g,'')}
// 用来判断对象是否为空
function isEmptyObject(e) {    var t;    for (t in e)        return !1;    return !0}
//格式化日期:yyyy-MM-dd     function formatDate(date) {   var myyear = date.getFullYear();   var mymonth = date.getMonth() + 1;   var myweekday = date.getDate();   if (mymonth < 10) {      mymonth = "0" + mymonth;   }   if (myweekday < 10) {      myweekday = "0" + myweekday;   }   return (myyear + "-" + mymonth + "-" + myweekday);}//获得某月的天数     function getMonthDays(myMonth) {   var monthStartDate = new Date(nowYear, myMonth, 1);   var monthEndDate = new Date(nowYear, myMonth + 1, 1);   var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);   return days;}//获得本季度的开始月份     function getQuarterStartMonth() {   var quarterStartMonth = 0;   if (nowMonth < 3) {      quarterStartMonth = 0;   }   if (2 < nowMonth && nowMonth < 6) {      quarterStartMonth = 3;   }   if (5 < nowMonth && nowMonth < 9) {      quarterStartMonth = 6;   }   if (nowMonth > 8) {      quarterStartMonth = 9;   }   return quarterStartMonth;}//获得本周的开始日期     function getWeekStartDate() {   var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);   return formatDate(weekStartDate);}//获得本周的结束日期     function getWeekEndDate() {   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));   return formatDate(weekEndDate);}//获得上周的开始日期     function getpreWeekStartDate() {   var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);   return formatDate(weekStartDate);}//获得上周的结束日期     function getpreWeekEndDate() {   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (-1 - nowDayOfWeek));   return formatDate(weekEndDate);}//获得本月的开始日期     function getMonthStartDate() {   var monthStartDate = new Date(nowYear, nowMonth, 1);   return formatDate(monthStartDate);}//获得本月的结束日期     function getMonthEndDate() {   var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));   return formatDate(monthEndDate);}//获得上月开始时间  function getLastMonthStartDate() {   var lastMonth2 = lastMonth;   var nowYear2 = nowYear;   if (nowMonth < lastMonth) {      var lastMonth2 = 11;      var nowYear2 = nowYear - 1;   }   var lastMonthStartDate = new Date(nowYear2, lastMonth2, 1);   return formatDate(lastMonthStartDate);}//获得上月结束时间  function getLastMonthEndDate() {   var lastMonth2 = lastMonth;   var nowYear2 = nowYear;   if (nowMonth < lastMonth) {      var lastMonth2 = 11;      var nowYear2 = nowYear - 1;   }   var lastMonthEndDate = new Date(nowYear2, lastMonth2, getMonthDays(lastMonth));   return formatDate(lastMonthEndDate);}//获得本季度的开始日期     function getQuarterStartDate() {   var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);   return formatDate(quarterStartDate);}//获得本季度的结束日期     function getQuarterEndDate() {   var quarterEndMonth = getQuarterStartMonth() + 2;   var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));   return formatDate(quarterStartDate);}//计算当天 昨天 前天function _getDay(day) {   var today = new Date();   var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;   today.setTime(targetday_milliseconds); //注意,这行是关键代码       var tYear = today.getFullYear();   var tMonth = today.getMonth();   var tDate = today.getDate();   tMonth = doHandleMonth(tMonth + 1);   tDate = doHandleMonth(tDate);   return tYear + "-" + tMonth + "-" + tDate;}function doHandleMonth(month) {   var m = month;   if (month.toString().length == 1) {      m = "0" + month;   }   return m;}

/*获取选中的文字*/

var _getSelectedText = function() {

if (window.getSelection) {

  return window.getSelection().toString();

} else if (document.getSelection) {

  return document.getSelection();

} else if (document.selection) {

  return document.selection.createRange().text;

}else{

  return "";

}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!