原生JS常用方法集合(持续更新)

1、工具类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var Util = {
    // 获取get参数
    getParam: function (name) {
        name = new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)');
        if (name = name.exec(location.search))
            return decodeURIComponent(name[1]);
        return ''
    },
    // 添加千位分隔符
    milliFormat: function (num) {
        return num && num.toString().replace(/^\d+/g, function (m) {
                return m.replace(/(?=(?!^)(\d{3})+$)/g, ',')
            });
    },
    // 获取字符串绝对长度
    absLength: function (str) {
        return str.replace(/[^\x00-\xff]/ig, '**').length;
    },
    // 复制内容到粘贴板
    copyContent: function (e) {
        if (e.hasAttribute('data-copy')) {
            var content = e.getAttribute('data-copy');
            if (window.clipboardData) { // IE
                window.clipboardData.setData('Text', content);
                console.log('copy success for ie');
            } else {
                const input = document.createElement('input');
                input.setAttribute('readonly', 'readonly');
                input.setAttribute('value', content);
                e.appendChild(input);
                input.focus();
                input.setSelectionRange(0, -1);
                if (document.execCommand('copy')) {
                    document.execCommand('copy');
                    console.log('copy success');
                }
                e.removeChild(input);
            }
        }
    },
    // 深拷贝
    deepCopy: function (p, c) {
        c = c || {};
        for (let i in p) {
            if (!p.hasOwnProperty(i)) {
                continue;
            }
            if (typeof p[i] === "object") {
                c[i] = (p[i].constructor === Array) ? [] : {};
                this.deepCopy(p[i], c[i]);
            } else {
                c[i] = p[i];
            }
        }
        return c;
    }
};

2、小数处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var Digital = {
    // 浮点数加法
    addFloat: function (num1, num2) {
        var num1Digits = (num1.toString().split('.')[1] || '').length;
        var num2Digits = (num2.toString().split('.')[1] || '').length;
        var baseNum = Math.pow(10, Math.max(num1Digits, num2Digits));
        return (num1 * baseNum + num2 * baseNum) / baseNum;
    },
    // 浮点数除法
    divFloat: function (num1, num2) {
        var t1 = 0, t2 = 0, r1, r2;
        try {
            t1 = num1.toString().split(".")[1].length;
        } catch (e) {
        }
        try {
            t2 = num2.toString().split(".")[1].length;
        } catch (e) {
        }
        r1 = Number(num1.toString().replace(".", ""));
        r2 = Number(num2.toString().replace(".", ""));
        var res = (r1 / r2) * Math.pow(10, t2 - t1);
        return isFinite(res) ? res : 0;
    },
    // 浮点数乘法
    mulFloat: function (num1, num2) {
        var m = 0, s1 = num1.toString(), s2 = num2.toString();
        try {
            m += s1.split(".")[1].length;
        } catch (e) {
        }
        try {
            m += s2.split(".")[1].length;
        }
        catch (e) {
        }
        var res = Number(s1.replace(".", ""))
            * Number(s2.replace(".", ""))
            / Math.pow(10, m);
        return isFinite(res) ? res : 0;
    },
};

发表评论

电子邮件地址不会被公开。 必填项已用*标注