首页 > 代码库 > Tool.js(javascript帮助类)
Tool.js(javascript帮助类)
//string.format
$.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; }
//获取URL参数
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; }
//Cookie
// 设置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 = http://www.mamicode.com/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); }
//运算
// 浮点数加法运算 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); } }
// html字符串转义
function htmlEscape(htmlString) { htmlString = htmlString.replace(/&/g, ‘&‘); htmlString = htmlString.replace(/</g, ‘<‘); htmlString = htmlString.replace(/>/g, ‘>‘); htmlString = htmlString.replace(/‘/g, ‘´‘); htmlString = htmlString.replace(/"/g, ‘"‘); htmlString = htmlString.replace(/\|/g, ‘¦‘); return htmlString; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。