首页 > 语言 > JavaScript > 正文

AppBaseJs 类库 网上常用的javascript函数及其他js类库写的

2024-05-06 14:11:13
字体:
来源:转载
供稿:网友
代码如下:
/*-----------------------------------
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() < 13 ? self.getHours() : (self.getHours() - 12));
case "h": return self.getHours() < 13 ? self.getHours() : (self.getHours() - 12);
case "HH": return p(self.getHours());
case "H": return self.getHours();
case "mm": return p(self.getMinutes());
case "m": return self.getMinutes();
case "ss": return p(self.getSeconds());
case "s": return self.getSeconds();
case "yyyy": return self.getFullYear();
case "yy": return self.getFullYear().toString().substring(2, 4);
case "dddd": return self.getDayName();
case "ddd": return self.getDayName(true);
case "dd": return p(self.getDate());
case "d": return self.getDate().toString();
case "MMMM": return self.getMonthName();
case "MMM": return self.getMonthName(true);
case "MM": return p((self.getMonth() + 1));
case "M": return self.getMonth() + 1;
case "t": return self.getHours() < 12 ? Date.CultureInfo.amDesignator.substring(0, 1) : Date.CultureInfo.pmDesignator.substring(0, 1);
case "tt": return self.getHours() < 12 ? Date.CultureInfo.amDesignator : Date.CultureInfo.pmDesignator;
case "zzz":
case "zz":
case "z": return "";
}
}) : this.toString();
};
/*------------------------------------*/

//声明对象
var App = {};
//对象继承或属性合并
App.extend = function(obj, hash) {
this.each(hash, function(key, value) {
obj[key] = value;
});
return obj;
};
//遍历
App.each = function(obj, func, context) {
var length = obj.length, i = -1;
if(length !== undefined) {
while(++i < length) if(func.call(context, obj[i], i, obj, length) === false) break;
}
else for(var key in obj) if(obj.hasOwnProperty(key)) if(func.call(context, key, obj[key], obj) === false) break;
return obj;
};
(function(doc, win){
var string = Object.prototype.toString,
quirks = doc.compatMode === "BackCompat",
docelem = doc.documentElement,
ua = win.navigator.userAgent.toLowerCase(),
version = (ua.match( /.(?:rv|it|ra|ie)[//: ]([/d.]+)/ ) || [])[1],
isChrome = /chrome/.test(ua),
isWebKit = /webkit/.test(ua),
isSafari = !isChrome && isWebKit,
isOpera = /opera/.test(ua),
isIE = /msie/.test( ua ) && !isOpera,
isFF = /firefox/.test(ua);
//Dom加载
doc.ready = function(func) {
var isReady = false,doReady = function() {
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选