JavaScript快速开发方法库
使用下面的方法可以节约大量的开发时间
1、Base
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//************************************************
//*Base
//************************************************
function $(a)
{
return typeof(a) == "string"?document.getElementById(a):a;
}
function $F(a)
{
if(!$(a))return "";
var tag = $(a).tagName.toUpperCase();
if(tag=="INPUT"||tag=="TEXTAREA"||tag=="SELECT")
{
return $(a).value;
}
else
{
return $(a).innerHTML;
}
}
function $T(o)
{
if(arguments.length==1)
{
return $T(document,o);
}
if(!$(o))return null;
return $(o).getElementsByTagName(arguments[1]);
}
2、dom
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//**********************************
//*dom
//**********************************
var Elem = {
New:function(a,d,c,i)//新建element,a:类型,d:id,c:,className,i:innerHTML
{
var o = document.createElement(a);
if(d)o.id = d;
if(c)o.className = c;
if(i)o.innerHTML = i;
return o;
},
Add:function(o)//为o元素追加子元素,可一个传进多个子元素
{
if(arguments.length==1)
{
Elem.Add(document.body,o);
return;
}
var o = $(o);
if(!o)return;
for (var i = 1; i < arguments.length; i++)
{
o.appendChild($(arguments[i]));
}
return o;
},
Del:function()//删除元素
{
for (var i = 0; i < arguments.length; i++)
{
if($(arguments[i]))
$(arguments[i]).parentNode.removeChild($(arguments[i]));
}
},
Hid:function()//隐藏
{
for (var i = 0; i < arguments.length; i++)
{
if($(arguments[i]))
$(arguments[i]).style.display="none";
}
},
Show:function()//显示
{
for (var i = 0; i < arguments.length; i++)
{
if($(arguments[i]))
$(arguments[i]).style.display="block";
}
},
Value:function(o,s)//赋值
{
s=s?s:"";
if(!$(o))return;
var tag = $(o).tagName.toUpperCase();
if(tag=="INPUT"||tag=="TEXTAREA"||tag=="SELECT")
{
$(o).value = s;
}
else
{
$(o).innerHTML = s;
}
},
Append:function(o,s)//追加内容
{
var tag = $(o).tagName.toUpperCase();
if(tag=="INPUT"||tag=="TEXTAREA")
{
$(o).value += s;
}
else
{
$(o).innerHTML += s;
}
},
Toggle:function()//原本显示则隐藏,反正也如此
{
for (var i = 0; i < arguments.length; i++)
{
if($(arguments[i]))
$(arguments[i]).style.display=$(arguments[i]).style.display=="none"?"block":"none";
}
},
Enable:function()//可用
{
for (var i = 0; i < arguments.length; i++)
{
$(arguments[i]).disabled="";
}
},
Disable:function()//不可用
{
for (var i = 0; i < arguments.length; i++)
{
$(arguments[i]).disabled="disabled";
}
},
Child:function(o)
{
return $(o).childNodes;
},
GetX:function(o,po)
{
for (var lx=0;o!=po;lx+=o.offsetLeft,o=o.offsetParent);
return lx;
},
GetY:function(o,po)
{
for (var ly=0;o!=po;ly+=o.offsetTop,o=o.offsetParent);
return ly;
},
Top:function()//屏幕可视范围离页面顶距离,放在这里是为了好管理
{
if(typeof(window.pageYOffset)!='undefined')
{
return window.pageYOffset;
}
else if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
{
return document.documentElement.scrollTop;
}
else if(typeof(document.body)!='undefined')
{
return document.body.scrollTop;
}
},
Left:function()//屏幕可视范围离左边距离
{
if(typeof(window.pageXOffset)!='undefined')
{
return window.pageXOffset;
}
else if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
{
return document.documentElement.scrollLeft;
}
else if(typeof(document.body)!='undefined')
{
return document.body.scrollLeft;
}
},
Width:function()//屏幕可视范围宽
{
if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
{
return document.documentElement.clientWidth;
}
else if(typeof(document.body)!='undefined')
{
return document.body.clientWidth;
}
},
Height:function()//屏幕可视范围高
{
if(typeof(document.compatMode)!='undefined'&&document.compatMode!='BackCompat')
{
return document.documentElement.clientHeight;
}
else if(typeof(document.body)!='undefined')
{
return document.body.clientHeight;
}
}
}
3、Event
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//*****************************************
//*Event
//*****************************************
var Evt = {
NoBubble:function(e)
{
e&&e.stopPropagation?e.stopPropagation():event.cancelBubble=true;
},
Top:function(e)
{
return (e||event).clientY;
},
Left:function(e)
{
return (e||event).clientX;
}
}
4、xml
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//******************************************
//*xml
//******************************************
var Xml = {
First:function(o,key)//第一个节点值
{
return $T(o,key)[0]&&$T(o,key)[0].firstChild?$T(o,key)[0].firstChild.nodeValue:"";
}
}
5、other
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//******************************************
//other
//******************************************
var Other = {
Even:function(n)//返回偶数
{
return parseInt((parseInt(n))/2)*2;
},
Break:function(o,len)//自动换行,用于ff
{
var strContent=$F(o);
var strTemp="";
while(strContent.length>len)
{
strTemp+=strContent.substr(0,len)+" ";
strContent=strContent.substr(len,strContent.length);
}
strTemp+=" "+strContent;
return strTemp;
},
Browser:function()
{
if(!!window.opera)
{
return "opera";
}
else if(navigator.userAgent.toLowerCase().indexOf("safari")>0)
{
return "safari";
}
else if(navigator.userAgent.toLowerCase().indexOf("gecko")>0)
{
return "firefox";
}
else
{
return "ie";
}
},
GetCookie:function(key)
{
var search=key+"=";
if(document.cookie.length>0)
{
var offset=document.cookie.indexOf(search);
if(offset!=-1)
{
offset+=search.length;
var end=document.cookie.indexOf(";",offset);
if(end==-1)end=document.cookie.length;
return unescape(document.cookie.substring(offset,end));
}
return "";
}
return "";
},
SetCookie:function(key,value)
{
var today=new Date();
var expires=new Date();
value = value.toString();
expires.setTime(today.getTime()+1000*60*60*24*365);
document.cookie=key+"="+value.escapeEx()+";path=/; expires="+expires.toGMTString();
},
TestCookie:function()
{
Other.SetCookie("test","");
var t = "test";
Other.SetCookie("test",t);
return Other.GetCookie("test")==t;
}
}
6、String扩展
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//******************************************
//String扩展
//******************************************
String.prototype.trim = function()
{
var str = this;
var m = str.match(/^\s*(\S+(\s+\S+)*)\s*$/);
return (m == null) ? "" : m[1];
}
String.prototype.strip = function()
{
return this.replace(/<\/?[^>]+>/gi, '').trim();
}
String.prototype.escapeHTML = function()
{
var div = document.createElement('div');
var text = document.createTextNode(this);
div.appendChild(text);
return div.innerHTML;
}
String.prototype.unescapeHTML = function()
{
var div = document.createElement('div');
div.innerHTML = this.stripTags();
return div.childNodes[0].nodeValue;
}
String.prototype.escapeEx = function()
{
return escape(this).replace(/\+/g,"%2b");
}
String.prototype.replaceAll = function(a,b)
{
return this.replace(new RegExp(a.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"g"),b);
}
String.prototype.indexOfEx = function()
{
var bi = arguments[arguments.length - 1];
var thisObj = this;
var idx = 0;
if(typeof(arguments[arguments.length - 2]) == 'number')
{
idx = arguments[arguments.length - 2];
thisObj = this.substr(idx);
}
var re = new RegExp(arguments[0],bi?'i':'');
var r = thisObj.match(re);
return r==null?-1:r.index + idx;
}
String.prototype.padLeft = function(str,n)
{
var result = this;
if(this.length<n)
for(var i=0;i<n-this.length;i++)
result = str+result;
return result;
}
7、Array扩展
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//******************************************
//Array扩展
//******************************************
Array.prototype.indexOf = function(obj)//返回一个对象在Array中的位置
{
var result = -1;
for(var i = 0; i < this.length; i++)
{
if(this[i] == obj)
{
result = i;
break;
}
}
return result;
}
Array.prototype.contains = function(obj)//检查一个对象是否包含在Array中
{
return this.indexOf(obj) > -1;
}
Array.prototype.add = function(obj)//添加一个对象
{
if(!(this.contains(obj)))
{
this[this.length] = obj;
}
}
Array.prototype.remove = function(obj)//删除一个对象
{
if(this.contains(obj))
{
var index = this.indexOf(obj);
for(var i = index; i < this.length - 1; i++)
{
this[i] = this[i + 1];
}
this.length--;
}
}
Array.prototype.clear = function()//清空数组
{
this.splice(0,this.length);
}
Array.prototype.value = function(s,l)//设置默认值
{
if(l)this.length=l;
for(var i = 0; i < this.length; i++)
{
this[i] = s;
}
}
8、StringBuilder
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//**********************************************************
//*StringBuilder
//**********************************************************
function StringBuilder()
{
this._arr = new Array();
this.add = function()
{
for(var i = 0;i<arguments.length;i++)
this._arr.push(arguments[i]);
}
this.toString = function()
{
return this._arr.join("");
}
}
9、Ajax
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
//**********************************************************
//*Ajax
//**********************************************************
function Ajax()
{
var _req = !!0;
try{_req=new XMLHttpRequest;}catch(e){try {_req=new ActiveXObject("MSXML2.XMLHTTP");}catch(e2){try {_req=new ActiveXObject("Microsoft.XMLHTTP");}catch(e3){_req=false;}}}
if (!_req) return;
this.req = _req;
this.send = function(_url,_content,_callback,_method,_isasync)
{
var Url = _url||"";
var Content = _content||"";
var Callback = _callback;
var Method = _method||"GET";
var IsAsync = _isasync==null?true:_isasync;
_req.open (Method,Url,IsAsync);
if(Method=="POST")_req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
_req.onreadystatechange = function()
{
if(_req.readyState==4&&Callback)
{
Callback(_req.status==200?_req:null);
}
};
_req.send(Method=="POST"?Content:"");
}
this.get = function(_url,_callback)
{
this.send(_url,"",_callback,"GET");
}
this.post = function(_url,_content,_callback)
{
this.send(_url,_content,_callback,"POST");
}
}