js方法封装处理

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

      }

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 大繁若简,大智如愚。夫妻也好,朋友也罢,相处之道最忌讳算计,我爱你本与你无关,比起我爱你,我更爱我爱你时我...
    芙蕖沉香阅读 1,864评论 0 0
  • 2016年真他妈的混蛋,日子过得狗都不如!
    受伤的雨阅读 1,495评论 0 0
  • oJc战无不胜阅读 1,670评论 0 0

友情链接更多精彩内容