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


    var util = {
        //返回上一个页面
        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存储,可用于过滤器调用
        GetTypeNameModel: function (op) {
            var model = {
                sessionStorageKey: op.sessionStorageKey,//此类型在本地session中key值
                apiUrl: op.apiUrl,//此类型列表获取接口
                idKey: op.idKey,//此类型在对象中id标识字段名
                nameKey: op.nameKey,//此类型在对象中显示名称字段名
                list: [],
                getList: function () {
                    var list = sessionStorage.getItem(model.sessionStorageKey);
                    if (list) {
                        model.list = JSON.parse(list);
                    } else {
                        ajaxProtogenesis.ajax("post", model.apiUrl, true, {}, function (res) {
                            model.list = res.RetData;
                            sessionStorage.setItem(model.sessionStorageKey, JSON.stringify(model.list));
                        })
                    }
                },
                getName: function (id) {//根据类型获取名称的方法
                    if (model.list == [] || model.length == 0) {
                        model.getList();
                    }
                    var res = '';
                    if (model.list.length > 0) {
                        for (var i = 0; i < model.list.length; i++) {
                            if (model.list[i][model.idKey] == id) {
                                res = model.list[i][model.nameKey];
                                break;
                            }
                        }
                    }
                    return res;
                }
            };
            model.getList();
            return model;
        },
    
        //数组转为二维数组形式成表格的行
        arrayToRow: function (model, all, rowNo, maxCell,defaultData) {
            //model行的数组,all所有数据数组,rowNo当前行下标,maxCell总列数,defaultData数据不存在时用默认值填充
    
            //获取当前这行数据
            var row = [];
            for (var i = 0; i < maxCell; i++) {
                if (all.length > rowNo * maxCell + i) {
                    row.push(all[rowNo * maxCell + i]);
                } else {
                    row.push(defaultData)
                }
            }
            model.push(row);
            rowNo++;//下一行下标
    
            if (all.length > rowNo * maxCell) {
                this.arrayToRow(model, all, rowNo, maxCell, defaultData);
            }
            return model;
        },
        //数组转为二维数组形式成表格
        arrayToTable: function (all, maxRow, maxCell,def) {
            //all所有数据数组,单张表maxRow总行数,maxCell总列数,def默认值。
    
            //每张表的数据数组
            var tableDatas = [];
            util.arrayToRow(tableDatas, all, 0, maxRow * maxCell, def);
    
            //为表获取行
            var tables = [];
            for (var i = 0; i < tableDatas.length; i++) {
                var rows = [];
                rows = util.arrayToRow(rows, tableDatas[i], 0, maxCell, def);
                tables[i] = rows;
            }
    
            return tables;
        },
        //数组转为二维数组形式成表格_示例
        arrayToTableTest: function () {
            var all = [];
            for (var i = 0; i < 97; i++) {
                all.push(i + 1);
            }
            var t96 = this.arrayToTable(all, 12, 8, {})
            console.log(t96);
            return t96;
        },
    
        //添加属性到util,用合并对象的方式,用来给全局调用的属性,但要注意属性名称冲突
        addAssign: function (obj) {
            Object.assign(util, obj);
        }
    };
    //获取样本类型名称方法对象
    util.TypeNameModel_SampleType = util.GetTypeNameModel({
        sessionStorageKey: "_sampleTypeList20210932",
        apiUrl: "/Product/ProductInfo/GetProductExtractionSampleDTO",
        idKey: "ExtractionSampleTypeId",
        nameKey: "SampleTypeName",
    });
    
    ////ng过滤器例:
    //FeiYeNGApp.filter("SampleType", function () {
    //    return function (input) {
    //        return util.TypeNameModel_SampleType.getName(input);
    //    }
    //});
    
    //微信小程序转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));
        }
    };
  • 相关阅读:
    cocos2d-x JS 定时器暂停方法
    cocos2d-x JS 本地玩家位置跟服务器玩家位置转换相关
    cocos creator 背景音乐音量和音效音量百分比设置
    iPhoneX快速适配,简单到你想哭。
    Cocos Creator Slider(进度条)的三种实现
    图片的本地存储和读取问题
    Creator仿超级玛丽小游戏源码分享
    cocos2d-x 贡献一个oss上传脚本
    Cocos Creator
    Cocos Creator cc.Node.点击事件
  • 原文地址:https://www.cnblogs.com/lanofsky/p/15357140.html
Copyright © 2020-2023  润新知