javascript 常用代碼大全(超級收藏,強(qiáng)烈推薦)(3)
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
打開模式對話框 [align=left]二、功能類 針對javascript的幾個(gè)對象的擴(kuò)充函數(shù) 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; } /* ****************************************** 日期函數(shù)擴(kuò)充 ****************************************** */ /* =========================================== //轉(zhuǎn)換成大寫日期(中文) =========================================== */ date.prototype.tocase = function() { var digits= new array('零','一','二','三','四','五','六','七','八','九','十','十一','十二'); var unit= new array('年','月','日','點(diǎn)','分','秒'); 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]; ///////得到時(shí) 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; } /* =========================================== //轉(zhuǎn)換成農(nóng)歷 =========================================== */ date.prototype.tochinese = function() { //暫缺 } /* =========================================== //是否是閏年 =========================================== */ date.prototype.isleapyear = function() { return (0==this.getyear()%4&&((this.getyear()%100!=0)||(this.getyear()%400==0))); } /* =========================================== //獲得該月的天數(shù) =========================================== */ 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; } /* =========================================== //由字符串直接實(shí)例日期對象 =========================================== */ date.prototype.instancefromstring = function(str) { return new date("2004-10-10".replace(/-/g, "\/")); } /* =========================================== //得到日期年月日等加數(shù)字后的日期 =========================================== */ 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; } } /* =========================================== //計(jì)算兩日期相差的日期年月日等 =========================================== */ date.prototype.datediff = function(interval,objdate) { //暫缺 } /* ****************************************** 數(shù)字函數(shù)擴(kuò)充 ****************************************** */ /* =========================================== //轉(zhuǎn)換成中文大寫數(shù)字 =========================================== */ 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("","拾","佰","仟","萬","億","點(diǎn)",""); 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) //加上小數(shù)部分(如果有小數(shù)部分) { re += bb[6]; for(var i=0; i } return re; } /* =========================================== //保留小數(shù)點(diǎn)位數(shù) =========================================== */ 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); } /* =========================================== //轉(zhuǎn)換成大寫金額 =========================================== */ 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; } /* ****************************************** 其他函數(shù)擴(kuò)充 ****************************************** */ /* =========================================== //驗(yàn)證類函數(shù) =========================================== */ 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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |