常用的 js 代码梳理

    xiaoxiao2022-06-30  96

    /** * 格式化时间函数 * @param {format} 时间显示格式 */ Date.prototype.format = function (format) { var date = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S+": this.getMilliseconds() }; if (/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; }; String.prototype.format = function (args) { if (arguments.length > 0) { var result = this; if (arguments.length == 1 && typeof (args) == "object") { for (var key in args) { var reg = new RegExp("({" + key + "})", "g"); result = result.replace(reg, args[key]); } } else { for (var i = 0; i < arguments.length; i++) { if (arguments[i] == undefined) { result = result.replace(reg, arguments[i]); } else { var reg = new RegExp("({[" + i + "]})", "g"); result = result.replace(reg, arguments[i]); } } } return result; } else { return this; } } String.prototype.trim = function () { return this.replace(/(^\s*)|(\s*$)/g, '') }; function StringBuilder() { this.__values = new Array(); }; StringBuilder.prototype.appendLine = function (v) { this.__values.push(v); } StringBuilder.prototype.toString = function () { return this.__values.join(''); } Number.prototype.toFixed = function (d) { var s = this + ""; if (!d) d = 0; if (s.indexOf(".") == -1) s += "."; s += new Array(d + 1).join("0"); if (new RegExp("^(-|\\+)?(\\d+(\\.\\d{0," + (d + 1) + "})?)\\d*$").test(s)) { var s = "0" + RegExp.$2, pm = RegExp.$1, a = RegExp.$3.length, b = true; if (a == d + 2) { a = s.match(/\d/g); if (parseInt(a[a.length - 1]) > 4) { for (var i = a.length - 2; i >= 0; i--) { a[i] = parseInt(a[i]) + 1; if (a[i] == 10) { a[i] = 0; b = i != 1; } else break; } } s = a.join("").replace(new RegExp("(\\d+)(\\d{" + d + "})\\d$"), "$1.$2"); } if (b) s = s.substr(1); return (pm + s).replace(/\.$/, ""); } return this + ""; } //限制只能键入数字,flage:是否验证‘.’传入则不可以输入‘.’ function entNumber(e, flage) { e = e || window.event; var keyCode = e.keyCode || e.which; if (!(keyCode == 46) && !(keyCode == 8) && !(keyCode == 37) && !(keyCode == 39) && !(keyCode == 17) && !(keyCode == 13) && ctrlKey()) { if (!((keyCode >= 48 && keyCode <= 57) || (keyCode == 110 || keyCode == 190) || keyCode == 9 || (keyCode >= 96 && keyCode <= 105))) stopDefault(e); if (flage) if (!((keyCode >= 48 && keyCode <= 57) || keyCode == 9 || (keyCode >= 96 && keyCode <= 105))) stopDefault(e); } //ctrl+c/v/a/x/z function ctrlKey() { return !(e.ctrlKey && keyCode == 67) && !(e.ctrlKey && keyCode == 86) && !(e.ctrlKey && keyCode == 65) && !(e.ctrlKey && keyCode == 88) && !(e.ctrlKey && keyCode == 90) } } function getKeyCode(e) { e = e || window.event; return e.keyCode || e.which; } //阻止浏览器的默认行为 function stopDefault(e) { e = e || window.event; if (e.preventDefault) e.preventDefault(); //其他浏览器 else e.returnValue = false; //IE浏览器 } /** * 阻止事件(包括冒泡和默认行为) * */ function stopEvent(e) { e = e || window.event; if (e.preventDefault) { //其他浏览器 e.preventDefault(); e.stopPropagation(); } else { //IE浏览器 e.returnValue = false; e.cancelBubble = true; } }; //过滤HTML标签 String.prototype.removeHtmlTab = function () { return this.replace(/<[^<>]+?>/g, ''); } //HTML标签字符转换成转意符 String.prototype.html2Escape = function () { return this.replace(/[<>&"]/g, function (c) { return { '<': '<', '>': '>', '&': '&', '"': '"'}[c]; }); } //转意符换成HTML标签 String.prototype.escape2Html = function () { var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' }; return this.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) { return arrEntities[t]; }); } // 转成空格 String.prototype.nbsp2Space = function () { var arrEntities = { 'nbsp': ' ' }; return this.replace(/&(nbsp);/ig, function (all, t) { return arrEntities[t] }) } //回车转为br标签 String.prototype.return2Br = function () { return this.replace(/\r?\n/g, "<br />"); } /** * 移除数组中的空元素 * @param {array} 数组 * @returns {narray} 新数组 * */ Array.prototype.trimArray = function () { var array = this; var narray = []; for (var i = 0; i < array.length; i++) if (array[i]) { if (typeof array[i] == "string") { if (array[i].trim()) narray.push(array[i]); } else { narray.push(array[i]); } } return narray; }; /** * 移除数组中的指定元素 * @param {elm} 指定元素值 * @returns {narray} 新数组 * */ Array.prototype.remove = function (elm) { for (var i = 0; i < this.length; i++) { if (this[i] == elm) this[i] == ''; break; } return this.trimArray(); //清除空元素 }; /** * 去除数组中重复的元素 * @param {isStrict} 是否严格模式 * ['1',1] isStric=true 返回1,否则,返回1,1 * @returns {Array} * */ Array.prototype.unique = function (isStrict) { if (!this.length) return []; if (this.length < 2) return [this[0]] || []; var tempObj = {}, newArr = []; for (var i = 0; i < this.length; i++) { var v = this[i]; var condition = isStrict ? (typeof tempObj[v] != typeof v) : false; if ((typeof tempObj[v] == "undefined") || condition) { tempObj[v] = v; newArr.push(v); } } return newArr; }; /** * 统计元素在数组中出现的次数 * @param {array} 数组 * ['1',1] isStric=true 返回1,否则,返回1,1 * @returns {Array} 返回一个二维数组::["元素名","出现的次数"] * */ Array.prototype.sameCount = function () { var res = []; var ary = this; ary.sort(); for (var i = 0; i < ary.length; ) { var count = 0; for (var j = i; j < ary.length; j++) { if (ary[i] == ary[j]) { count++; } } res.push([ary[i], count]); i += count; } return res; }

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-1126189.html

    最新回复(0)