1、设置缓存
// 设置缓存
Set_Storage(data_id: any, data: any) {
if (data_id) {
if (data >= 0 || data) {
const lsobj: any = window.sessionStorage;
const datajson: any = JSON.stringify(data);
lsobj.setItem(data_id, datajson);
}
}
}
2、获取缓存
Get_Storage(data_id: any): any {
if (data_id) {
let data: any = null;
const lsdata = window.sessionStorage;
let datajson: any = lsdata.getItem(data_id);
datajson = JSON.parse(datajson);
data = datajson;
return data;
}
}
3、// 获取url中含"?"符后的字串
GetRequest(name: any): any {
const _url = window.location.href;
const url = _url.slice(_url.indexOf('?'));
const theRequest: any = new Object();
if (url.indexOf('?') !== -1) {
let str: any = url.substr(1);
str = str.split('&');
for (let i = 0; i < str.length; i ++) {
theRequest[str[i].split('=')[0]] = decodeURI(str[i].split('=')[1]);
}
}
return theRequest[name];
}
4、 // 获取当前时间,格式YYYY-MM-DD
getNowFormatDate() {
const date = new Date();
const seperator1 = '-';
const year = date.getFullYear();
let month: any = date.getMonth() + 1;
let strDate: any = date.getDate();
if (month >= 1 && month <= 9) {
month = '0' + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = '0' + strDate;
}
const currentdate = year + seperator1 + month + seperator1 + strDate;
return currentdate;
}
5、// 获取url目录
getRealPath() {
const obj = window.location;
const contextPath = obj.pathname.split('/')[1] ? '/' + obj.pathname.split('/')[1] : '';
const basePath = obj.protocol + '//' + obj.host + contextPath;
return basePath;
}
6、 /* 校验各种输入规则
参数说明:
checkObj:需校验的数据
checkFlag:校验的规则类型
*/
checkData(checkObj: any , checkFlag: any ) {
if (checkObj) {
let checkData: any = checkObj;
checkData = checkData.replace(' ', '');
checkData = checkData.replace('¥', '').replace('$', '');
let exreg: any;
switch (checkFlag) {
case 1 :
exreg = /^[1-9][0-9]*\.{0,1}(\d{1,2})?$/; // 正数(正数或浮点数,最多两位小数)
break;
case 2 :
exreg = /^[1-9][0-9]*\.{0,1}(\d{1,2})?$/; // 正数(正数或浮点数,最多两位小数)
break;
}
if ( checkFlag === 1) {
if (!exreg.test(checkData)) {
return false;
} else {
if ( Number(checkData) > 3000) {
return false;
}
}
} else if ( checkFlag === 2 ) {
if (!exreg.test(checkData)) {
return false;
}
}
return true;
}
}
7、js获取元素及元素属性值(兼容ie)
getStyleAttr(obj,attr){
if(window.getComputedStyle){
return window.getComputedStyle(obj)[attr];
}else{
return obj.currentStyle[attr];
}
}