常用js函数
根据数组下标删除元素
Array.prototype.remove = function (from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); };
时间格式化
Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } 用法: new Date().Format("yyyy-MM-dd hh:mm:ss")
获取url中参数值
/** * 获取url中值 * * @param key * 参数 * @returns {null} */ getUrlParam : function(key) { var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; }
判断字符串是否为空值
/** * 判断是否为空 * * @param v * @returns {boolean} */ isNull : function(v) { var tag = false; if (v == null || v == undefined || v == "" || v.length == 0) { tag = true; } return tag; }
本地存储
/** * 设置本地存储 */ setLS : function(k, v) { localStorage.setItem(k, JSON.stringify(v)); }, /** * 获取本地存储 */ getLS : function(k) { return JSON.parse(localStorage.getItem(k)); }
分页算法
pagination:function(curPage,totalPage){ var page = {} var numPages = []; // 当前页 var cp = curPage; // 总共页 var tp = totalPage; // 展示多少页 var sp = 7; // 起始页 var bp; // 结束页 var ep; if (sp % 2 == 0) sp = sp + 1; var dp = Math.floor(sp / 2); var dif = tp - sp; var f = cp - dp; var g = tp - (cp + dp); if (sp >= 0 && dif >= 0) { if (g > 0) { if (f > 0) { bp = f; ep = cp + dp; } else { bp = 1; ep = 2 * dp + 1; } } else { bp = tp - 2 * dp; ep = tp; } } else { bp = 1; ep = tp; } var html ='<a onclick="gotoPage(1)">首页</a>'; if(curPage == 1){ html += '<a class="a_on" >上一页</a>'; }else{ html += '<a onclick="gotoPage('+(curPage -1)+')" >上一页</a>'; html += '<a class="a_on">......</a>'; } for (var i = bp; i <= ep; i++) { if(i == curPage){ html += '<a class="a_on">'+i+'</a>'; }else{ html += '<a onclick="gotoPage('+i+')">'+i+'</a>'; } //numPages.push(i); } if(curPage == totalPage){ html += '<a class="a_on">下一页</a>' }else{ html += '<a class="a_on">......</a>'; html += '<a onclick="gotoPage('+(curPage +1)+')">下一页</a>' } html += '<a onclick="gotoPage('+totalPage+')">尾页</a>'; if(totalPage == 0 || curPage == 0){ $("#pageList").html(""); return; } $("#pageList").html(html); }
公共弹出框
/** * 提示弹出框 */ alert:function(text){ var html = ''; html +='<div class="pop-alert">'; html +='<div class="flexBox">'; html +='<div class="box-alert">'; html +='<div class="box-title-alert">提示</div>'; html +='<div class="box-content-alert">'+text+'</div>'; html +='<div class="btn-row-alert"><a class="close layer-btn-alert">确定</a></div>'; html +='</div></div></div>'; $("body").append(html); $(".layer-btn-alert").click(function(){ $(".pop-alert").remove(); $('.layer-btn-alert').unbind("click"); }) }, /** * 确认弹出框 * text 显示内容 * callBack 确认回调函数 */ confirm:function(text,callBack){ var html = ''; html +='<div class="pop-alert">'; html +='<div class="flexBox">'; html +='<div class="box-alert">'; html +='<div class="box-title-alert">确认提示</div>'; html +='<div class="box-content-alert">'+text+'</div>'; html +='<div class="btn-row-alert"><a class="close layer-btn-confirm" id="layer-btn-sure">确定</a><a class="close layer-btn-confirm" id="layer-btn-cancel">取消</a></div>'; html +='</div></div></div>'; $("body").append(html); $("#layer-btn-cancel").click(function(){ $(".pop-alert").remove(); $('#layer-btn-cancel').unbind("click"); }) $("#layer-btn-sure").click(function(){ $(".pop-alert").remove(); $('#layer-btn-sure').unbind("click"); callBack(); }) } /*提示弹出框样式**/ .pop-alert { position: fixed; left: 0; right: 0; top: 0; bottom: 0; z-index: 11; -webkit-animation: fadeIn .4s both; animation: fadeIn .4s both;} .pop-alert .flexBox { height: 100%; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; background: rgba(0, 0, 0, .2); } .box-alert { width: 300px; height: auto; background: #fff; border-radius: 4px; position: relative; } .layer-btn-alert { display: block;margin: 5px auto;width: 80px;height: 30px;line-height: 30px;border-radius: 5px;background: #428bca;border: 0;color: #fff;font-size: 16px;text-align: center;cursor: pointer; } .box-title-alert{border-bottom:1px solid #ddd; height:40px; line-height:40px; font-size:16px; color:#F3B402; padding-left:5px} .box-content-alert{line-height:30px; font-size:16px; padding:15px 5px} .btn-row-alert{border-top:1px solid #ddd} .layer-btn-confirm {display: block;float:right; margin: 5px 35px;width: 80px;height: 30px;line-height: 30px;border-radius: 5px;background: #428bca;border: 0;color: #fff;font-size: 16px;text-align: center;cursor: pointer; }
校验身份证
/** * 校验身份证 * @param code 身份证号 * @returns {{code: boolean, msg: string}} */ cardIdValid: function (code) { var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " }; var tip = ""; var pass = true; if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) { tip = "身份证号格式错误"; pass = false; } else if (!city[code.substr(0, 2)]) { tip = "身份证地址编码错误"; pass = false; } else { //18位身份证需要验证最后一位校验位 if (code.length == 18) { code = code.split(''); //∑(ai×Wi)(mod 11) //加权因子 var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; //校验位 var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2]; var sum = 0; var ai = 0; var wi = 0; for (var i = 0; i < 17; i++) { ai = code[i]; wi = factor[i]; sum += ai * wi; } var last = parity[sum % 11]; if (parity[sum % 11] != code[17]) { tip = "身份证校验位错误"; pass = false; } } } return {code: pass, msg: tip}; }
校验手机号
/** * 校验手机号 * @param phone * @returns {{code: boolean, msg: string}} */ phoneValid: function (phone) { var tag = true; var tip = "" if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) { tip = "手机号码有误,请重填"; tag = false; } return {code: tag, msg: tip}; }
获取手机终端信息
/**获取移动终端浏览器版本信息*/ browser: { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部 }; }(), language: (navigator.browserLanguage || navigator.language).toLowerCase() }
将总秒数转换为天、时、分、秒
/** * @param value 秒数 * @returns {string} 返回倒计时天、时、分、秒 */ formatSeconds: function (value) { var theTime = parseInt(value);// 秒 var theTime1 = 0;// 分 var theTime2 = 0;// 小时 var theTime3 = 0;// 天 if (theTime > 60) { theTime1 = parseInt(theTime / 60); theTime = parseInt(theTime % 60); if (theTime1 > 60) { theTime2 = parseInt(theTime1 / 60); theTime1 = parseInt(theTime1 % 60); } if (theTime2 > 24) { theTime3 = parseInt(theTime2 / 24); theTime2 = parseInt(theTime2 % 24); } } var result = "" + parseInt(theTime) + "秒"; if (theTime1 > 0) { result = "" + parseInt(theTime1) + "分" + result; } if (theTime2 > 0) { result = "" + parseInt(theTime2) + "小时" + result; } if (theTime3 > 0) { result = "" + parseInt(theTime3) + "天" + result; } return result; }