Tool.js(javascript帮助类)

 //string.format

Tool.js(javascript帮助类)
 $.format = function (source, params) {
            if (arguments.length == 1)
                return function () {
                    var args = $.makeArray(arguments);
                    args.unshift(source);
                    return $.validator.format.apply(this, args);
                };
            if (arguments.length > 2 && params.constructor != Array) {
                params = $.makeArray(arguments).slice(1);
            }
            if (params.constructor != Array) {
                params = [params];
            }
            $.each(params, function (i, n) {
                source = source.replace(new RegExp("\\{" + i + "\\}", "g"), (typeof n == undefined || n == null) ? ‘‘ : n);
            });
            return source;
        }
View Code

//获取URL参数

Tool.js(javascript帮助类)
 function request(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return r[2]; return null;
        }
View Code

//Cookie

Tool.js(javascript帮助类)
 // 设置Cookie
        function setCookie(name, value) {
            var expires = (arguments.length > 2) ? arguments[2] : null;
            document.cookie = name + "=" + encodeURIComponent(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + "";
        }

        // 获取Cookie
        function getCookie(name) {
            var value = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (value != null) {
                return decodeURIComponent(value[2]);
            } else {
                return null;
            }
        }

        // 删除cookie
        function removeCookie(name) {
            var expires = new Date();
            expires.setTime(expires.getTime() - 1000 * 60);
            setCookie(name, "", expires);
        }
View Code

//运算

Tool.js(javascript帮助类)
  // 浮点数加法运算
        function floatAdd(arg1, arg2) {
            var r1, r2, m;
            try {
                r1 = arg1.toString().split(".")[1].length;
            } catch (e) {
                r1 = 0;
            }
            try {
                r2 = arg2.toString().split(".")[1].length;
            } catch (e) {
                r2 = 0;
            }
            m = Math.pow(10, Math.max(r1, r2));
            return (arg1 * m + arg2 * m) / m;
        }

        // 浮点数减法运算
        function floatSub(arg1, arg2) {
            var r1, r2, m, n;
            try {
                r1 = arg1.toString().split(".")[1].length;
            } catch (e) {
                r1 = 0
            }
            try {
                r2 = arg2.toString().split(".")[1].length;
            } catch (e) {
                r2 = 0
            }
            m = Math.pow(10, Math.max(r1, r2));
            n = (r1 >= r2) ? r1 : r2;
            return ((arg1 * m - arg2 * m) / m).toFixed(n);
        }

        // 浮点数乘法运算
        function floatMul(arg1, arg2) {
            var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
            try {
                m += s1.split(".")[1].length;
            } catch (e) { }
            try {
                m += s2.split(".")[1].length;
            } catch (e) { }
            return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
        }

        //保留2位四舍五入
        function changeTwoDecimal_f(x) {
            var f_x = parseFloat(x);
            if (isNaN(f_x)) {
                alert(function:changeTwoDecimal->parameter error);
                return false;
            }
            var f_x = Math.round(x * 100) / 100;
            var s_x = f_x.toString();
            var pos_decimal = s_x.indexOf(.);
            if (pos_decimal < 0) {
                pos_decimal = s_x.length;
                s_x += .;
            }
            while (s_x.length <= pos_decimal + 2) {
                s_x += 0;
            }
            return s_x;
        }

        // 浮点数除法运算
        function floatDiv(arg1, arg2) {
            var t1 = 0, t2 = 0, r1, r2;
            try {
                t1 = arg1.toString().split(".")[1].length;
            } catch (e) { }
            try {
                t2 = arg2.toString().split(".")[1].length;
            } catch (e) { }
            with (Math) {
                r1 = Number(arg1.toString().replace(".", ""));
                r2 = Number(arg2.toString().replace(".", ""));
                return (r1 / r2) * pow(10, t2 - t1);
            }
        }

        // 设置数值精度
        function setScale(value, scale, roundingMode) {
            if (roundingMode.toLowerCase() == "roundhalfup") {
                return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            } else if (roundingMode.toLowerCase() == "roundup") {
                return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            } else {
                return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
            }
        }
View Code

// html字符串转义

Tool.js(javascript帮助类)
 function htmlEscape(htmlString) {
            htmlString = htmlString.replace(/&/g, &amp;);
            htmlString = htmlString.replace(/</g, &lt;);
            htmlString = htmlString.replace(/>/g, &gt;);
            htmlString = htmlString.replace(//g, &acute;);
            htmlString = htmlString.replace(/"/g, ‘&quot;‘);
            htmlString = htmlString.replace(/\|/g, &brvbar;);
            return htmlString;
        }
View Code

 

Tool.js(javascript帮助类),布布扣,bubuko.com

Tool.js(javascript帮助类)

上一篇:ubuntu-18.04 设置开机启动脚本-亲测有效


下一篇:Binder机制,从Java到C (6. Binder in Native : libbinder)