js方法封裝處理

1、設(shè)置緩存

// 設(shè)置緩存

? ? 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、 // 獲取當(dāng)前時間,格式Y(jié)YYY-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、 /* 校驗各種輸入規(guī)則

? ? 參數(shù)說明:

? ? checkObj:需校驗的數(shù)據(jù)

? ? checkFlag:校驗的規(guī)則類型

? ? */

? ? 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})?$/; // 正數(shù)(正數(shù)或浮點數(shù),最多兩位小數(shù))

? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case 2 :

? ? ? ? ? ? ? ? exreg =? /^[1-9][0-9]*\.{0,1}(\d{1,2})?$/; // 正數(shù)(正數(shù)或浮點數(shù),最多兩位小數(shù))

? ? ? ? ? ? ? ? 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];

? ? ? }

? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 大繁若簡,大智如愚。夫妻也好,朋友也罷,相處之道最忌諱算計,我愛你本與你無關(guān),比起我愛你,我更愛我愛你時我...
    芙蕖沉香閱讀 301評論 0 0
  • 2016年真他媽的混蛋,日子過得狗都不如!
    受傷的雨閱讀 214評論 0 0
  • oJc戰(zhàn)無不勝閱讀 256評論 0 0

友情鏈接更多精彩內(nèi)容