首页 > 代码库 > 封装方法
封装方法
<script lang ge="javascript">
// 合并多个空白为一个空白
String.prototype.resetBlank = function()
{
var regEx = /\s+/g;
return this.replace(regEx,‘ ‘);
}// 除去左边空白
String.prototype.Ltrim = function()
{
var regEx = /^\s+/g;
return this.replace(regEx,‘‘);
}
// 除去右边空白
String.prototype.Rtrim = function()
{
var regEx = /\s+$/g;
return this.replace(regEx,‘‘);
}
// 除去两边空白
String.prototype.Trim = function()
{
var regEx = /(^\s+)|(\s+$)/g;
return this.replace(regEx,‘‘);
}
// 除去所有空白
String.prototype.Trim = function()
{
var regEx = /\s*/g;
return this.replace(regEx,‘‘);
}
// 保留数字
String.prototype.GetNum = function()
{
var regEx = /[^\d]/g;
return this.replace(regEx,‘‘);
}// 保留字母
String.prototype.GetEn = function()
{
var regEx = /[^A-Za-z]/g;
return this.replace(regEx,‘‘);
}// 保留大写字母
String.prototype.Get?se = function()
{
var regEx = /[^A-Z]/g;
return this.replace(regEx,‘‘);
}
// 保留小写字母
String.prototype.GetLcase = function()
{
var regEx = /[^a-z]/g;
return this.replace(regEx,‘‘);
}// 保留中文
String.prototype.GetCn = function()
{
var regEx = /[^\一-\龥\?-\?]/g;
return this.replace(regEx,‘‘);
}// 得到子节长度
String.prototype.GetRealLength = function()
{
var regEx = /^[\一-\龥\?-\?]+$/;
if (regEx.test(this))
{
return this.length * 2;
} else {
var oMatches = this.match(/[\x00-\xff]/g);
var oLength = this.length * 2 - oMatches.length;
return oLength;
}
}// 左边截取字符串
String.prototype.Left = function(oLength)
{
return this.slice(0,oLength);
}// 右边截取字符串
String.prototype.Right = function(oLength)
{
if (this.length<oLength)
{
return this;
} else {
return this.slice(this.length-oLength);
}
}// 获取文件全名
String.prototype.GetFileName = function()
{
var regEx = /^.*\/([^\/\?]*).*$/;
return this.replace(regEx,‘$1‘);
}// 获取文件扩展名
String.prototype.GetExtensionName = function()
{
var regEx = /^.*\/[^\/]*(\.[^\.\?]*).*$/;
return this.replace(regEx,‘$1‘);
}
// 获取文本内容
String.prototype.GetInnerText = function()
{
var regEx = /<\/?[^>]*>/g;
return this.replace(regEx,‘‘);
}// 监测是否为空
String.prototype.isEmail = function()
{
return (this==‘‘);
}// 监测是否相等
String.prototype.isEq ls = function(oString)
{
return (this==oString);
}// 监测邮箱格式
String.prototype.isEmail = function()
{
var regEx = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
return regEx.test(this);
}// String转化为Number
String.prototype.Int = function()
{
return isNaN(parseInt(this)) ? this.toString() : parseInt(this);
}// HTML编码
String.prototype.HTMLEncode = function()
{
var objThis = this;
var oRegExp = new Array(/\x26/g,/\x3c/g,/\x3e/g,/\x3e/g,/\x27/g);
var oTarget = new Array(‘&‘,‘<‘,‘>‘,‘"‘,‘\‘‘);
for (var i=0; i<5; i++)
{
objThis = objThis.replace(oRegExp, oTarget);
}
return objThis;
}// 字符串格式化
String.prototype.encodeStr = function()
{
var objThis = this;
objThis = objThis.HTMLEncode();
objThis = objThis.replace(/\n/g, ‘<br>‘);
objThis = objThis.replace(/\t/g, ‘ ‘);
objThis = objThis.replace(/\s/g, ‘ ‘);
return objThis;
}// Unicode转化
String.prototype.AscW = function()
{
var strText = ‘‘;
for (var i=0; i<this.length; i++)
{
strText += ‘&#‘ + this.charCodeAt(i);
}
return strText;
}// 数字补零
Number.prototype.LengthWithZero = function(oCount)
{
var strText = this.toString();
while (strText.length<oCount)
{
strText = ‘0‘ + strText;
}
return strText;
}
// Unicode还原
Number.prototype.ChrW = function()
{
return String.fromCharCode(this);
}// 数字数组由小到大排序
Array.prototype.Min2Max = function()
{
var oVal;
for (var i=0; i<this.length; i++)
{
for (var j=0; j<=i; j++)
{
if (this<this[j])
{
oVal = this;
this = this[j];
this[j] = oVal;
}
}
}
return this;
}// 数字数组由大到小排序
Array.prototype.Max2Min = function()
{
var oVal;
for (var i=0; i<this.length; i++)
{
for (var j=0; j<=i; j++)
{
if (this>this[j])
{
oVal = this;
this = this[j];
this[j] = oVal;
}
}
}
return this;
}// 获得数组值
Array.prototype.Item = function(oIndex)
{
return this[oIndex];
}// 删除数组指定项
Array.prototype.removeItem = function(oIndex)
{
this.splice(oIndex,1);
}// 正向查找
Array.prototype.InStr = function(oTarget)
{
for (var i=0; i<this.length; i++)
{
if (this=oTarget && typeof(this)==typeof(oTarget))
{
return i;
}
}
return -1;
}// 反向查找
Array.prototype.LastInStr = function(oTarget)
{
for (var i=this.length-1; i>=0; i--)
{
if (this=oTarget && typeof(this)==typeof(oTarget))
{
return i;
}
}
return -1;
}// 判断是否存在
Array.prototype.InStrBolean = function(oTarget)
{
for (var i=0; i<this.length; i++)
{
if (this.oTarget && typeof(this)==typeof(oTarget))
{
return tr;
}
}
return false;
}// 获得数字数组中最大项
Array.prototype.GetMax = function()
{
var oVal = 0;
for(var i=0; i<this.length; i++)
{
if (this>oVal)
{
oVal = this;
}
}
return oVal;
}// 获得数字数组中最小项
Array.prototype.GetMin = function()
{
var oVal = 0;
for(var i=0; i<this.length; i++)
{
if (this<oVal)
{
oVal = this;
}
}
return oVal;
}// 获取当前时间的GB形式
Date.prototype.GetCNDate = function()
{
var oDateText = ‘‘;
oDateText += this.getFullYear().LengthWithZero(4) + new Number(24180).ChrW();
oDateText += this.getMonth().LengthWithZero(2) + new Number(26376).ChrW();
oDateText += this.getDate().LengthWithZero(2) + new Number(26085).ChrW();
oDateText += this.getHours().LengthWithZero(2) + new Number(26102).ChrW();
oDateText += this.getMinutes().LengthWithZero(2) + new Number(20998).ChrW();
oDateText += this.getSeconds().LengthWithZero(2) + new Number(31186).ChrW();
oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String(‘26085199682010819977222352011620845‘).s str(this.getDay()*5,5).Int().ChrW();
return oDateText;
}// 得到对象的绝对坐标
function GetPosition(obj)
{
var objThis = obj;
var oBody = document.body;
var oLeft = oTop = 0;
while (objThis!=oBody)
{
oLeft += objThis.offsetLeft;
oTop += objThis.offsetTop;
objThis = objThis.offsetParent;
}
return { left: oLeft, top: oTop };
}// 取一定范围的随机数
function GetRndNum(lLimit, uLimit)
{
var oRndNum = Math.floor((uLimit-lLimit+1)*Math.random()+lLimit);
return oRndNum;
}// 获取指定位数的随机密码
function GetRndPwd(oLength)
{
var oRndPwd = ‘‘;
var oTempNum;
var oArray = new Array(‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,‘y‘,‘z‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘);
while (oRndPwd.length<oLength)
{
oTempNum = GetRndNum(0,61);
oRndPwd += oArray[oTempNum].toString();
}
return oRndPwd;
}// 定义圆类
function Circle(xPoint, yPoint, oRadius)
{
this.X = xPoint;
this.Y = yPoint;
this.R = oRadius;
this.PI = Math.PI;
this.circleArea = function()
{
return this.PI * this.R * this.R;
}
this.circleLength = function()
{
return 2 * this.PI * this.R;
}
}
</script>