JS常用操作拓展 1.获取路径URL参数 2.时间格式化处理 3.字符串截断返回省略号 4.判断是否IE浏览器及版本 5.时间戳中文转化 6.手机号码校验 7.邮箱校验 8.获取背景图片URL地址
1.获取路径URL参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] === variable) { return pair[1]; } } return ''; }
用法示例:var productId = getQueryVariable('productId');
2.时间格式化处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function dateFtt(fmt, date) { if (typeof (date) === 'string') date = new Date(date); var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "h+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.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; }
用法示例1:var time = dateFtt('yyyy-MM-dd hh:mm:ss', '2020-06-12T11:00:14.27')
用法示例2:var time = dateFtt('yyyy-MM-dd', '2017/01/09 00:00:00')
3.字符串截断返回省略号
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function cutString(str, cutLen) { if (!str || str.length < cutLen) { return str } else { return str.substring(0, cutLen) + '...' } }
用法示例:var text= cutString('大家好才是真的好',5);
4.判断是否IE浏览器及版本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function IEVersion() { var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器 var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器 var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1; if (isIE) { var reIE = new RegExp("MSIE (\\d+\\.\\d+);"); reIE.test(userAgent); var fIEVersion = parseFloat(RegExp["$1"]); if (fIEVersion == 7) { return 7; } else if (fIEVersion == 8) { return 8; } else if (fIEVersion == 9) { return 9; } else if (fIEVersion == 10) { return 10; } else { return 6;//IE版本<=7 } } else if (isEdge) { return 'edge';//edge } else if (isIE11) { return 11; //IE11 } else { return -1;//不是ie浏览器 } }
用法示例:if(IEVersion()>8) //这是IE8以上浏览器
5.时间戳中文转化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function timeReplacement(argument = '') { if (argument == null || argument == '') { return '' } let date = argument; return new Date(date).getFullYear() + "年" + (new Date(date).getMonth() + 1) + "月" + new Date(date).getDate() + "日"; }
用法示例:var textTime = timeReplacement('2017/01/09 00:00:00') //返回 2017年1月9日
6.手机号码校验
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function regPhoneNumber(argument = '') { let reg = /^[1][1-9][0-9]{9}$/ argument = argument.replace(/\s+/g, '') if (!(reg.test(argument)) || argument.length === 0) { return false } else { return true } }
用法示例:if(regPhoneNumber('18888888888')) //手机号码正确
7.邮箱校验
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
function regmMailboxFun(argument = '') { let reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/; argument = argument.replace(/\s+/g, ''); if (reg.test(argument)) { return true } else { return false } }
用法示例:if(regmMailboxFun('1688@163.com')) //邮箱输入正确
8.获取背景图片URL地址
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
$('#').css("backgroundImage").replace('url(','').replace(')','')
用法示例:var bgurl = $('#').css("backgroundImage").replace('url(','').replace(')','')