js常用方法使用总结
来源:互联网 发布:ant构建 tomcat源码 编辑:程序博客网 时间:2024/06/06 04:17
说明:
- 在项目中使用一些工具类,公共类是非常有必要的,不仅是后台,前段亦是一样
- 这里提供我收集的常用方法封装
- 代码可以在资源共享,我的代码中下载。
注意:
- 字符串的拼接一定使用StringBuffer来拼接,否则容易造成浏览器卡顿或内存溢出。特别是针对一些执行js效率不高的浏览器!!
- 经常对输入框里内容清空,对textarea,可以直接
("textarea").empty();如果使用 (“textarea”).html(“”);也可能会造成浏览器内存溢出!!
Date工具类
/********************** date工具类 ***************/Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.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;};
公共工具类
/********************** 公共工具类 ***************/var PublicUtil ={ isNotEmpty: function(val){ return !this.isEmpty(val); }, isEmpty: function(val){ if ((val==null || typeof(val)=="undefined")|| (typeof(val)=="string"&&val==""&&val!="undefined")){ return true; }else{ return false; } }, isDebug: function(){ if(this.isNotEmpty(configDebug)&&configDebug=="true"){ return true; }else{ return false; } }, //去除元素内所有内容 strIds:"#id1,#id2,#id3" emptyHtml: function(strIds){ try{ var ids = strIds.trim(",").split(","); $(ids).each(function(){ var obj = $(this.toString()); if(obj.length>0){ $(obj).each(function(){ $(this).html(""); }); }else{ obj.html(""); } }); }catch(ex){ if(PublicUtil.isDebug()){ throw new Error("js方法:【PublicUtil.emptyHtml(strIds)】,error!"); } } }, //去除元素的值 strIds:"#id1,#id2,#id3" emptyValue: function(strIds){ try{ var ids = strIds.trim(",").split(","); $(ids).each(function(){ var obj = $(this.toString()); if(obj.length>0){ $(obj).each(function(){ $(this).val(""); }); }else{ obj.val(""); } }); }catch(ex){ if(PublicUtil.isDebug()){ throw new Error("js方法:【PublicUtil.emptyValue(strIds)】,error!"); } } }, //去除Textarea内所有内容 strIds:"#id1,#id2,#id3" emptyTextarea: function(strIds){ try{ var ids = strIds.trim(",").split(","); $(ids).each(function(){ var obj = $(this.toString()); if(obj.length>0){ $(obj).each(function(){ $(this).empty(); $(this).val(""); }); }else{ obj.empty(); obj.val(""); } }); }catch(ex){ if(PublicUtil.isDebug()){ throw new Error("js方法:【PublicUtil.emptyTextarea(strIds)】,error!"); } } }}
String 工具类
/********************** String工具类***************///trim去掉字符串两边的指定字符,默去空格String.prototype.trim = function(tag) { if (!tag) { tag = '\\s'; }else { if (tag == '\\') { tag = '\\\\'; } else if (tag == ',' || tag == '|' || tag == ';') { tag = '\\' + tag; }else { tag = '\\s'; } } eval('var reg=/(^' + tag + '+)|(' + tag + '+$)/g;'); return this.replace(reg, '');};//字符串截取后面加入...String.prototype.interceptString = function(len) { if (this.length > len) { return this.substring(0, len) + "..."; } else { return this; }}//将一个字符串用给定的字符变成数组String.prototype.toArray = function(tag) { if (this.indexOf(tag) != -1) { return this.split(tag); }else { if (this != '') { return [this.toString()]; }else { return []; } }}//只留下数字(0123456789)String.prototype.toNumber= function() { return this.replace(/\D/g, ""); }//保留中文 String.prototype.toCN= function() { var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; return this.replace(regEx, ''); }//转成intString.prototype.toInt= function() { var temp = this.replace(/\D/g, ""); return isNaN(parseInt(temp)) ? this.toString() : parseInt(temp); }//是否是以XX开头String.prototype.startsWith= function(tag){ return this.substring(0, tag.length) == tag;}//是否已XX结尾String.prototype.endWith= function(tag){ return this.substring(this.length - tag.length) == tag;}//StringBuffervar StringBuffer = function() { this._strs = new Array; };StringBuffer.prototype.append = function (str) { this._strs.push(str); }; StringBuffer.prototype.toString = function() { return this._strs.join(""); };String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,"gm"),s2);}
Arry
/********************** Arry ***************///根据数据取得再数组中的索引Array.prototype.getIndex = function(obj){ for (var i = 0; i < this.length; i++) { if (obj == this[i]) { return i; } } return -1;}//移除数组中的某元素Array.prototype.remove= function (obj) { for (var i = 0; i < this.length; i++) { if (obj == this[i]) { this.splice(i, 1); break; } } return this;}//判断元素是否在数组中Array.prototype.contains= function (obj) { for (var i = 0; i < this.length; i++) { if (obj == this[i]) { return true; } } return false;}
浏览器相关操作
/********************** 浏览器相关操作 ***************///进入全屏模式, 判断各种浏览器,找到正确的方法var launchFullScreen = function (element) { if(element.requestFullscreen) { element.requestFullscreen(); } else if(element.mozRequestFullScreen) { element.mozRequestFullScreen(); } else if(element.webkitRequestFullscreen) { element.webkitRequestFullscreen(); } else if(element.msRequestFullscreen) { element.msRequestFullscreen(); } return true;}//退出全屏模式var exitFullScreen = function () { if(document.exitFullscreen) { document.exitFullscreen(); } else if(document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if(document.webkitExitFullscreen) { document.webkitExitFullscreen(); } return false;}//cookie操作var CookieUtil={ path: "/", domain: 'demo.j2ee.com', add: function(name,val){ $.cookie(name, val, {expires: 7, path: this.path, domain: this.domain, secure: true}); }, remove: function(name){ $.cookie(name, null,{path: this.path, domain: this.domain}); }, get: function(name){ $.cookie(name,{path: this.path, domain: this.domain}); }}//errorvar error={ e_404: function(){ alertMessage("404","未找到改页面!","warning"); }, e_500: function(){ alertMessage("500","服务器内部错误!","error"); }, e_403: function(){ alertMessage("403","权限不足!","warning"); }}
阅读全文
0 0
- js常用方法使用总结
- 常用js方法总结
- 常用js方法总结
- js 常用方法总结
- js常用方法总结
- js常用方法总结
- 常用js 工具方法总结
- Underscore.js常用方法总结
- JS数组常用方法总结
- js 数组常用方法总结
- js数组常用方法总结
- JS字符串常用方法总结
- js string()常用方法总结
- web js常用方法总结
- js字符串常用方法总结
- js中常用方法总结
- 【常用函数使用总结】js
- Linq 常用方法使用总结
- 【秋天再见】2017秋季回顾--- “图论”“程序员”“红帽”比赛
- 正则表达式,日期 日历
- Machine Learning(4)—Lessons by Andrew Ng
- 谈谈JS的观察者模式(自定义事件)
- unity和HTML通信
- js常用方法使用总结
- 对于常用字符编码格式(ASCII/Unicode/UTF-8)简介
- js简单代码生成器
- SDNU__1082.观妹种菊
- java验证身份证
- webview支持H5中的选择图片方法
- 阈值操作
- 阿里Dubbo疯狂更新,关Spring Cloud什么事?
- test