【js】【笔记】普通js常用

1、普通js常用

var util = {
 
    _sampleTypeListSessionKey: "_sampleTypeList20210930",//样本类型列表本地存储键值

    //返回上一个页面
    breakpage: function (p) {
        if (p == undefined) {
            p = -1;
        }
        window.location.href = "javascript:history.go(" + p + ")";
    },

    convertTime: function (val) {
        var date;
        if (typeof val == 'string' && val.indexOf("Date") <= -1) {
            date = new Date(val);
        } else {
            var re = /-?\d+/;
            var m = re.exec(val);
            date = new Date(parseInt(m[0]));
        }
        return date;
    },

    formatTime: function (val, format) {
        if (val == '' || val == null) {
            return '';
        }
        var date;
        if (typeof val == 'string' && val.indexOf("Date") <= -1) {
            date = new Date(val);
        } else if (typeof val == 'object'){
            //时间格式
            date = val;
        }
        else {
            var re = /-?\d+/;
            var m = re.exec(val);
            date = new Date(parseInt(m[0]));
        }
        if (format || format != null) {
            var o = {
                "M+": date.getMonth() + 1, //month
                "d+": date.getDate(), //day
                "H+": date.getHours(), //hour
                "m+": date.getMinutes(), //minute
                "s+": date.getSeconds(), //second
                "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
                "S": date.getMilliseconds() //millisecond
            }
            if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
                (date.getFullYear() + "").substr(4 - RegExp.$1.length));

            for (var k in o)
                if (new RegExp("(" + k + ")").test(format))
                    format = format.replace(RegExp.$1,
                        RegExp.$1.length == 1 ? o[k] :
                            ("00" + o[k]).substr(("" + o[k]).length));
        }
        return format;
    },

    //表单提交重定向
    formSubmitRedrict: function (url, datas, method, target, bc) {
        var tempFormName = 'tempForm';
        for (var i = 0; document.getElementById(tempFormName) != undefined; i++) {
            tempFormName = 'tempForm' + i;
        }
        var tempForm = document.createElement("form");
        tempForm.id = tempFormName;
        tempForm.method = method == undefined ? "post" : method;
        tempForm.action = url;
        tempForm.target = target == undefined ? "" : target;
        for (var i = 0; i < datas.length; i++) {
            var hideInput = document.createElement("input");
            hideInput.type = "hidden";
            hideInput.name = datas[i].name;
            hideInput.value = datas[i].value;
            tempForm.appendChild(hideInput);
        }

        if (bc == undefined) {
            bc = function () { }
        }
        if (document.all) {
            tempForm.attachEvent("onsubmit", bc);        //IE
        } else {
            tempForm.addEventListener("submit", bc);    //firefox
        }
        document.body.appendChild(tempForm);
        if (document.all) {
            tempForm.fireEvent("onsubmit");
        } else {
            tempForm.dispatchEvent(new Event("submit"));
        }
        tempForm.submit();
        document.body.removeChild(tempForm);
    },

    //copy到剪切板
    copyText: function (data, cb) {
        //data==={text: OrderNo,target:$event.target }
        //创建隐藏的标签
        var textArea;
        try {
            textArea = document.createElement("<textarea></textarea>");
        } catch (e) {
            textArea = document.createElement("textarea");
        }
        textArea.textContent = data.text;

        //不显示出来
        textArea.style.width = '1px';
        textArea.style.height = '1px';
        textArea.style.color = textArea.style.backgroundColor;

        //定位,不滚动
        if (!data.target) {
            data.target = document.body;
        }

        data.target.appendChild(textArea);

        textArea.select(); // 选择对象

        var res = false;
        try {
            res = document.execCommand("copy");//复制
        } catch (err) {
            data.target.removeChild(textArea);
            console.log('该浏览器不支持点击复制到剪贴板');
        }

        if (cb && typeof cb == "function") {
            cb(res)
        }
        data.target.removeChild(textArea);
    },

    //获取样本类型列表,保存在 浏览器session
    getSampleTypeList: function () {
        if (this._sampleTypeList.length <= 0) {
            var list = sessionStorage.getItem(this._sampleTypeListSessionKey);
            if (list) {
                this._sampleTypeList = JSON.parse(list);
                return this._sampleTypeList;
            }
            var _sampleTypeListSessionKey = this._sampleTypeListSessionKey;
            ajaxProtogenesis.ajax("post", "/Product/ProductInfo/GetProductExtractionSampleDTO", true, {}, function (res) {

                this._sampleTypeList = res.RetData;
                sessionStorage.setItem(_sampleTypeListSessionKey, JSON.stringify(this._sampleTypeList));
            })
        }
        return this._sampleTypeList;
    },
    _sampleTypeList: [],

    //获取名称公共方法,用于过滤器调用
    getSampleTypeName: function (id) {
        var list = this.getSampleTypeList();
        for (var i = 0; i < list.length; i++) {
            if (list[i].ExtractionSampleTypeId == id) {
                return list[i].SampleTypeName;
            }
        }
        return "";
    },
};

 

2、//微信小程序转ng电脑端,复制代码做部分中间处理

var wx = {
    //本地缓存
    setStorageSync: function (key, value) {
        if (typeof value == "object") {
            value = JSON.stringify(value);
        }
        localStorage.setItem(key, value);
    },
    getStorageSync: function (key) {
        var value = localStorage.getItem(key);
        try {
            return JSON.parse(value);
        } catch (e) {
        }
        return value;
    },
    removeStorageSync: function (key) {
        localStorage.removeItem(key);
    },

    //跳转
    navigateTo: function (data) {
        document.location.href = data.url;
    },
    navigateBack: function (data) {
        if (!data) {
            util.breakpage(-1);
            return;
        }
        util.breakpage((0 - data.delta));
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章