/*-----------------------------------
Web Application JavaScript Library
2009.11 janchie
------------------------------------*/
//String原生对象扩展 置空左右端空格
String.prototype.trim = function(){
return this.replace(/(^[\s\n\t\r]*)|([\s\n\r\t]*$)/g, "");
};
//Date原生对象扩展 格式化输出
Date.prototype.format = function (string) {
var self = this;
var p = function p(s) {
return (s.toString().length == 1) ? "0" s : s;
};
return string ? string.replace(/dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?|tt?|zz?z?/g,
function (string) {
switch (string) {
case "hh": return p(self.getHours() 0){
if(typeof attrValue === "undefined" || (attVal && attVal.test(attr))){
result.push(elems[i]);
}
}
}
return result;
},
//取body元素
$B: doc.body || docelem,
//取Class属性元素集合
$C:function(attrValue, tag, root){
return this.$A("className",attrValue, tag, root);
},
//取浏览器窗体宽度
getWinWidth: win.innerWidth || docelem.clientWidth || doc.body.clientWidth,
//取浏览器窗体高度
getWinHeight: win.innerHeight || docelem.clientHeight || doc.body.clientHeight,
//取元素样式
getStyle: function(elem,name){
if(elem.style[name]){
return elem.style[name];
}else if(elem.currentStyle){
return elem.currentStyle[name];
}else if(doc.defaultView && doc.defaultView.getComputedStyle){
name = name.replace(/([A-Z])/g,"-");
name = name.toLowerCase();
var s = doc.defaultView.getComputedStyle(elem,"");
return s && s.getPropertyValue(name);
}else{
return null;
}
},
//获取元素屏幕坐标值
getPosition: function() {
return docelem.getBoundingClientRect && function(o){
var pos = o.getBoundingClientRect(), root = o.ownerDocument || o.doc;
return {left:pos.left root.documentElement.scrollLeft,top:pos.top root.documentElement.scrollTop};
} || function(o){
var x = 0, y = 0;
do{x = o.offsetLeft;y = o.offsetTop;}while((o=o.offsetParent));
return {left:x,top:y};
};
}(),
//设置透明度
setOpacity: function (elem,num){
if(elem.filters){
elem.style.filter = "alpha(opacity=" num ")";
}else{
elem.style.opacity = num/100;
}
},
//隐藏或显示元素
hide: function(elem){elem.style.display = "none";},
show: function(elem){elem.style.display = "block";},
toggle: function(elem){
elem.style.display = this.getStyle(elem,"display") === "none" ?"block":"none";
},
//元素Class属性操作
addClass: function(elem, clsName) {
if (elem.className === '') {
elem.className = clsName;
}else if (elem.className !== '' && (' ' elem.className ' ').indexOf(' ' clsName ' ') === -1) {
elem.className = elem.className ' ' clsName;
}
},
removeClass: function(elem, clsName) {
if (clsName && (' ' elem.className ' ').indexOf(' ' clsName ' ') > -1) {
elem.className = (' ' elem.className ' ').replace(' ' clsName ' ', ' ').replace(/^ | $/g,'');
}
},
//追加Html文本对象( 支持Table )
append: function(elem, text) {
if (typeof text === "string") {
if (elem.insertAdjacentHTML){
if (elem.tagName === "TABLE"){
var html = elem.outerHTML,ep = elem.parentNode,sl = html.length;
text = html.substr(0,sl-8) text html.substr(sl-8,sl);
ep.insertAdjacentHTML("beforeEnd", text);
ep.replaceChild(ep.lastChild,elem);
}else{
elem.insertAdjacentHTML("beforeEnd", text);
}
}else {
var rlt = null, rg = doc.createRange(), fm = rg.createContextualFragment(text);
rlt ? elem.insertBefore(fm, rlt) : elem.appendChild(fm);
}
}else if (typeof text === "object") elem.appendChild(text);
},
//删除元素
remove:function(elem){
if (elem.parentNode) elem.parentNode.removeChild(elem);
},
//置空元素内容及子节点
empty:function(elem){
while(elem.firstChild){
elem.removeChild(elem.firstChild);
}
},
//图像预加载
loadimages: function(){
var a = arguments,loads = function(){
if(doc.images){ if(!doc.ps) doc.ps = [];
var i,j=doc.ps.length; for(i=0; ix2) x=x2;
obj.style.left = x "px";
overEvent && overEvent(x);
}
function up(){
obj.releaseCapture && obj.releaseCapture();
docelem.onmousemove = null;
docelem.onmouseup = null;
upEvent && upEvent(x);
}
},
//绑定竖向滚动事件
sliderY : function (obj,y1,y2,overEvent,upEvent){
var y, t , ut = this;
obj.onmousedown = function (e){
e = e || win.event;
ut.preventDefault(e);
obj.setCapture && obj.setCapture();
t = ut.getXY(e).y - parseInt(obj.style.top);
docelem.onmousemove = over;
docelem.onmouseup = up;
}
function over(e){
e = e || win.event;
y = ut.getXY(e).y - t;
if(yy2) y=y2;
obj.style.top = y "px";
overEvent && overEvent(y);
}
function up(){
obj.releaseCapture && obj.releaseCapture();
docelem.onmousemove = null;
docelem.onmouseup = null;
upEvent && upEvent(y);
}
},
//设置cookie
setCookie:function(n, v, t){
var exp = new Date();
exp.setTime(exp.getTime() (t||24)*60*60*1000);
doc.cookie = n "=" escape(v) ";expires=" exp.toGMTString() ';path=/';
},
//获取cookie
getCookie:function(n){
var arr = doc.cookie.match(new RegExp("(^| )" n "=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]);
return null;
}
});
})(document,window);
//日期字符串格转日期
App.parseDate = function(date){
var dt = date instanceof Date ? date: Date(date.replace("-","/"));
return isNaN(dt.getTime()) ? null : dt ;
};
//Json字符串转对象
App.parseJSON = function(jsonString) {
var result = false;
try {
result = eval('(' jsonString ')');
}catch (e) {};
return result;
};
//取不重复唯一值
App.getUid = function(){
return "uid" (new Date()).getTime() parseInt(Math.random()*100000);
};
//获取指定范围的随机数
App.random = function (n1, n2){
return Math.floor(Math.random()*(n2-n1 1)) n1;
};
//秒转换为毫秒
App.s2ms = function (str){
var t = str.split(":");
return t[0] * 60000 t[1] * 1000;
};
//毫秒转换为秒
App.ms2s = function (ms){
return (ms/60000 ":" ms/1000`).replace(/\.\d /g,"").replace(/(^|:)(\d)(?!\d)/g,"");
};
//数字转换为编号
App.num2number = function (num, n){
return Array(n).join("0").concat(num).slice(-n);
};
//数字转化为中文
App.num2gb = function (n){
return "零一二三四五六七八九".split("")[n];
};
//Flash生成代码
App.getFlash = function (url, width, height, param){
var tagName = "", o1 = {width:width||1, height:height||1}, o2 = {};
if (this.isIE){
tagName = "object ";
o1.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
o1.codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0";
o2.movie = url;
o2.quality = "high";
param && this.extend(o2, param);
}else{
tagName = "embed ";
o1.type = "application/x-shockwave-flash";
o1.pluginspage = "http://www.adobe.com/go/getflashplayer_cn";
o1.src = url;
o1.quality = "high";
param && this.extend(o1, param);
}
if(o1.width');
return '' a2.join('') '' tagName '>';
};
//播放器生成代码
App.getPlayer = function (url, width, height, param){
var wmp = ["6bf52a52-394a-11d3-b153-00c04f79faa6","application/x-mplayer2"];
var rmp = ["CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA","audio/x-pn-realaudio-plugin"];
var mp = /\.rm$/.test(url) ? rmp : wmp;
var tagName = "", o1 = {width:width||1, height:height||1}, o2 = {};
if (this.isIE){
tagName = "object ";
o1.classid = "clsid:" mp[0];
o2.url = url;
param && this.extend(o2, param);
}else{
tagName = "embed ";
o1.type = mp[1];
o1.src = url;
param && this.extend(o1, param);
}
if(o1.width');
return '' a2.join('') '' tagName '>';
};
//获取XMLHttp对象
App.xmlhttp = function (){
if (this.isFF) return new XMLHttpRequest();
var a = ["Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP","Microsoft.XMLHTTP","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.5.0"];
for (var i=0,l=a.length;i
有少量函数未经测试,欢迎大家提个意见