javascript 常用代码大全(超级收藏,强烈推荐)(3)
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
打开模式对话框 [align=left]二、功能类 针对javascript的几个对象的扩充函数 function checkbrowser() { this.ver=navigator.appversion this.dom=document.getelementbyid?1:0 this.ie6=(this.ver.indexof("msie 6")>-1 && this.dom)?1:0; this.ie5=(this.ver.indexof("msie 5")>-1 && this.dom)?1:0; this.ie4=(document.all && !this.dom)?1:0; this.ns5=(this.dom && parseint(this.ver) >= 5) ?1:0; this.ns4=(document.layers && !this.dom)?1:0; this.mac=(this.ver.indexof('mac') > -1) ?1:0; this.ope=(navigator.useragent.indexof('opera')>-1); this.ie=(this.ie6 || this.ie5 || this.ie4) this.ns=(this.ns4 || this.ns5) this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns5 || this.ns4 || this.mac || this.ope) this.nbw=(!this.bw) return this; } /* ****************************************** 日期函数扩充 ****************************************** */ /* =========================================== //转换成大写日期(中文) =========================================== */ date.prototype.tocase = function() { var digits= new array('零','一','二','三','四','五','六','七','八','九','十','十一','十二'); var unit= new array('年','月','日','点','分','秒'); var year= this.getyear() + ""; var index; var output=""; ////////得到年 for (index=0;index output += digits[parseint(year.substr(index,1))]; } output +=unit[0]; ///////得到月 output +=digits[this.getmonth()] + unit[1]; ///////得到日 switch (parseint(this.getdate() / 10)) { case 0: output +=digits[this.getdate() % 10]; break; case 1: output +=digits[10] + ((this.getdate() % 10)>0?digits[(this.getdate() % 10)]:""); break; case 2: case 3: output +=digits[parseint(this.getdate() / 10)] + digits[10] + ((this.getdate() % 10)>0?digits[(this.getdate() % 10)]:""); default: break; } output +=unit[2]; ///////得到时 switch (parseint(this.gethours() / 10)) { case 0: output +=digits[this.gethours() % 10]; break; case 1: output +=digits[10] + ((this.gethours() % 10)>0?digits[(this.gethours() % 10)]:""); break; case 2: output +=digits[parseint(this.gethours() / 10)] + digits[10] + ((this.gethours() % 10)>0?digits[(this.gethours() % 10)]:""); break; } output +=unit[3]; if(this.getminutes()==0&&this.getseconds()==0) { output +="整"; return output; } ///////得到分 switch (parseint(this.getminutes() / 10)) { case 0: output +=digits[this.getminutes() % 10]; break; case 1: output +=digits[10] + ((this.getminutes() % 10)>0?digits[(this.getminutes() % 10)]:""); break; case 2: case 3: case 4: case 5: output +=digits[parseint(this.getminutes() / 10)] + digits[10] + ((this.getminutes() % 10)>0?digits[(this.getminutes() % 10)]:""); break; } output +=unit[4]; if(this.getseconds()==0) { output +="整"; return output; } ///////得到秒 switch (parseint(this.getseconds() / 10)) { case 0: output +=digits[this.getseconds() % 10]; break; case 1: output +=digits[10] + ((this.getseconds() % 10)>0?digits[(this.getseconds() % 10)]:""); break; case 2: case 3: case 4: case 5: output +=digits[parseint(this.getseconds() / 10)] + digits[10] + ((this.getseconds() % 10)>0?digits[(this.getseconds() % 10)]:""); break; } output +=unit[5]; return output; } /* =========================================== //转换成农历 =========================================== */ date.prototype.tochinese = function() { //暂缺 } /* =========================================== //是否是闰年 =========================================== */ date.prototype.isleapyear = function() { return (0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0))); } /* =========================================== //获得该月的天数 =========================================== */ date.prototype.getdaycountinmonth = function() { var mon = new array(12); mon[0] = 31; mon[1] = 28; mon[2] = 31; mon[3] = 30; mon[4] = 31; mon[5] = 30; mon[6] = 31; mon[7] = 31; mon[8] = 30; mon[9] = 31; mon[10] = 30; mon[11] = 31; if(0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0))&&this.getmonth()==2) { return 29; } else { return mon[this.getmonth()]; } } /* =========================================== //日期比较 =========================================== */ date.prototype.compare = function(objdate) { if(typeof(objdate)!="object" && objdate.constructor != date) { return -2; } var d = this.gettime() - objdate.gettime(); if(d>0) { return 1; } else if(d==0) { return 0; } else { return -1; } } /* =========================================== //格式化日期格式 =========================================== */ date.prototype.format = function(formatstr) { var str = formatstr; str=str.replace(/yyyy|yyyy/,this.getfullyear()); str=str.replace(/yy|yy/,(this.getyear() % 100)>9?(this.getyear() % 100).tostring():"0" + (this.getyear() % 100)); str=str.replace(/mm/,this.getmonth()>9?this.getmonth().tostring():"0" + this.getmonth()); str=str.replace(/m/g,this.getmonth()); str=str.replace(/dd|dd/,this.getdate()>9?this.getdate().tostring():"0" + this.getdate()); str=str.replace(/d|d/g,this.getdate()); str=str.replace(/hh|hh/,this.gethours()>9?this.gethours().tostring():"0" + this.gethours()); str=str.replace(/h|h/g,this.gethours()); str=str.replace(/mm/,this.getminutes()>9?this.getminutes().tostring():"0" + this.getminutes()); str=str.replace(/m/g,this.getminutes()); str=str.replace(/ss|ss/,this.getseconds()>9?this.getseconds().tostring():"0" + this.getseconds()); str=str.replace(/s|s/g,this.getseconds()); return str; } /* =========================================== //由字符串直接实例日期对象 =========================================== */ date.prototype.instancefromstring = function(str) { return new date("2004-10-10".replace(/-/g, "\/")); } /* =========================================== //得到日期年月日等加数字后的日期 =========================================== */ date.prototype.dateadd = function(interval,number) { var date = this; switch(interval) { case "y" : date.setfullyear(date.getfullyear()+number); return date; case "q" : date.setmonth(date.getmonth()+number*3); return date; case "m" : date.setmonth(date.getmonth()+number); return date; case "w" : date.setdate(date.getdate()+number*7); return date; case "d" : date.setdate(date.getdate()+number); return date; case "h" : date.sethours(date.gethours()+number); return date; case "m" : date.setminutes(date.getminutes()+number); return date; case "s" : date.setseconds(date.getseconds()+number); return date; default : date.setdate(d.getdate()+number); return date; } } /* =========================================== //计算两日期相差的日期年月日等 =========================================== */ date.prototype.datediff = function(interval,objdate) { //暂缺 } /* ****************************************** 数字函数扩充 ****************************************** */ /* =========================================== //转换成中文大写数字 =========================================== */ number.prototype.tochinese = function() { var num = this; if(!/^\d*(\.\d*)?$/.test(num)){alert("number is wrong!"); return "number is wrong!";} var aa = new array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); var bb = new array("","拾","佰","仟","萬","億","点",""); var a = (""+ num).replace(/(^0*)/g, "").split("."), k = 0, re = ""; for(var i=a[0].length-1; i>=0; i--) { switch(k) { case 0 : re = bb[7] + re; break; case 4 : if(!new regexp("0{4}\\d{"+ (a[0].length-i-1) +"}$").test(a[0])) re = bb[4] + re; break; case 8 : re = bb[5] + re; bb[7] = bb[5]; k = 0; break; } if(k%4 == 2 && a[0].charat(i+2) != 0 && a[0].charat(i+1) == 0) re = aa[0] + re; if(a[0].charat(i) != 0) re = aa[a[0].charat(i)] + bb[k%4] + re; k++; } if(a.length>1) //加上小数部分(如果有小数部分) { re += bb[6]; for(var i=0; i } return re; } /* =========================================== //保留小数点位数 =========================================== */ number.prototype.tofixed=function(len) { if(isnan(len)||len==null) { len = 0; } else { if(len<0) { len = 0; } } return math.round(this * math.pow(10,len)) / math.pow(10,len); } /* =========================================== //转换成大写金额 =========================================== */ number.prototype.tomoney = function() { // constants: var maximum_number = 99999999999.99; // predefine the radix characters and currency symbols for output: var cn_zero= "零"; var cn_one= "壹"; var cn_two= "贰"; var cn_three= "叁"; var cn_four= "肆"; var cn_five= "伍"; var cn_six= "陆"; var cn_seven= "柒"; var cn_eight= "捌"; var cn_nine= "玖"; var cn_ten= "拾"; var cn_hundred= "佰"; var cn_thousand = "仟"; var cn_ten_thousand= "万"; var cn_hundred_million= "亿"; var cn_symbol= ""; var cn_dollar= "元"; var cn_ten_cent = "角"; var cn_cent= "分"; var cn_integer= "整"; // variables: var integral; // represent integral part of digit number. var decimal; // represent decimal part of digit number. var outputcharacters; // the output result. var parts; var digits, radices, bigradices, decimals; var zerocount; var i, p, d; var quotient, modulus; if (this > maximum_number) { return ""; } // process the coversion from currency digits to characters: // separate integral and decimal parts before processing coversion: parts = (this + "").split("."); if (parts.length > 1) { integral = parts[0]; decimal = parts[1]; // cut down redundant decimal digits that are after the second. decimal = decimal.substr(0, 2); } else { integral = parts[0]; decimal = ""; } // prepare the characters corresponding to the digits: digits= new array(cn_zero, cn_one, cn_two, cn_three, cn_four, cn_five, cn_six, cn_seven, cn_eight, cn_nine); radices= new array("", cn_ten, cn_hundred, cn_thousand); bigradices= new array("", cn_ten_thousand, cn_hundred_million); decimals= new array(cn_ten_cent, cn_cent); // start processing: outputcharacters = ""; // process integral part if it is larger than 0: if (number(integral) > 0) { zerocount = 0; for (i = 0; i < integral.length; i++) { p = integral.length - i - 1; d = integral.substr(i, 1); quotient = p / 4; modulus = p % 4; if (d == "0") { zerocount++; } else { if (zerocount > 0) { outputcharacters += digits[0]; } zerocount = 0; outputcharacters += digits[number(d)] + radices[modulus]; } if (modulus == 0 && zerocount < 4) { outputcharacters += bigradices[quotient]; } } outputcharacters += cn_dollar; } // process decimal part if there is: if (decimal != "") { for (i = 0; i < decimal.length; i++) { d = decimal.substr(i, 1); if (d != "0") { outputcharacters += digits[number(d)] + decimals; } } } // confirm and return the final output string: if (outputcharacters == "") { outputcharacters = cn_zero + cn_dollar; } if (decimal == "") { outputcharacters += cn_integer; } outputcharacters = cn_symbol + outputcharacters; return outputcharacters; } number.prototype.toimage = function() { var num = array( "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x5,0x5,0x5,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0x4,0x4,0x4,0x4,0x4}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x4,0xf,0x1,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x4,0xf,0x4,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0x5,0x5,0xf,0x4,0x4}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x1,0xf,0x4,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x1,0xf,0x5,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x4,0x4,0x4,0x4}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x5,0xf,0x5,0xf}", "#define t_width 3\n#define t_height 5\nstatic char t_bits[] = {0xf,0x5,0xf,0x4,0xf}" ); var str = this + ""; var iindex var result="" for(iindex=0;iindex result +=" } return result; } /* ****************************************** 其他函数扩充 ****************************************** */ /* =========================================== //验证类函数 =========================================== */ function isempty(obj) { obj=document.getelementsbyname(obj).item(0); if(trim(obj.value)=="") { if(obj.disabled==false && obj.readonly==false) { obj.focus(); } return true; } else { return false; } } /* =========================================== //无模式提示对话框 =========================================== */ function modelessalert(msg) { window.showmodelessdialog("javascript:alert(\""+escape(msg)+"\");window.close();","","status:no;resizable:no;h 该文章在 2010/4/26 23:18:39 编辑过 |
关键字查询
相关文章
正在查询... |