Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1510235
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: JavaScript

2013-06-08 08:11:16


点击(此处)折叠或打开

  1. //=================================== javascript ===================================

  2. //---- 自定义函数, 函数应该放在闭包外面, 放在闭包里面掉用不了 -----------

  3. //substr(start[,length])表示从start位置开始取length个字符串
  4. //substring(start,end)表示从start,到end之间的字符串,包括start位置的字符但是不包括end位置的字符
  5. //1.start不一定就是第一个参数,end也不一定就是第二个参数,substring(3,1)时,开始位置是1,结束位置是3;
  6. //2.当要返回的子字符串是从开始位置到结束时,end的值必须大于等于字符串的长度,
  7. //如上边的str.substring(7,8),按照索引从0开始算的话end的最大值为7,但这边却用8,当然,使用大于8的数返回的结果也是一样的,这点比较有意思。

  8. function dec_hex(i)
  9. {
  10.    var s = "0123456789ABCDEF";
  11.    var ss = s.substr(i/16,1) + s.substr(i%16,1); //获取子字符串

  12.    return(ss);
  13. }

  14. function hex_dec(s)
  15. {
  16.     var arg = [0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0,0,10,11,12,13,14,15];        //数组

  17.     s = s.toUpperCase(s);
  18.     var pos1 = s.charCodeAt(0) - 48;        //取字符串的ASCII
  19.     var pos2 = s.charCodeAt(1) - 48;        //'A'的ASCII为65, -48=17, 即arg[17]=10
  20.     var Rtn = arg[pos1]*16 + arg[pos2];

  21.     return(Rtn);
  22. }

  23. function GetCRC8(p, Num)        //p为数组传递参数, Num为数据的长度
  24. {
  25.  var crcarray = [
  26.      0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
  27.       157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
  28.        35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
  29.       190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
  30.        70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
  31.       219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
  32.       101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
  33.       248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
  34.       140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
  35.        17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
  36.       175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
  37.        50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
  38.       202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
  39.        87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
  40.       233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
  41.       116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53];
  42.  var temp,CRC8 = 0;

  43.  for(var i=0; i<Num; i++)
  44.         CRC8 = crcarray[CRC8 ^ p[i]];

  45.  return(CRC8);
  46. }

  47. var calCRC16 = function(strSrc)
  48. {
  49.     //strSrc = "010200C40016";
  50.     var len = strSrc.length;
  51.     var reg_crc = 0xffff;
  52.     var m_Crc;
  53.     for(var i=0 ;i <len/2;i++)
  54.     {
  55.         var c = strSrc.substr(2*i,2);
  56.         var r = parseInt(c,16);
  57.         reg_crc ^= r;
  58.         for(var j=0;j<8;j++)
  59.         {
  60.             m_Crc = reg_crc;
  61.             reg_crc>>=1;
  62.             if(m_Crc&0x0001)
  63.             {
  64.                 reg_crc ^= 0xa001;
  65.             }
  66.         }

  67.     }
  68.     var temp = reg_crc.toString(16);
  69.     return (temp.substr(2,2)+temp.substr(0,2)).toUpperCase();
  70. }

  71. function DateToString(datetime)
  72. {
  73.  var year = datetime.getFullYear();
  74.  var month = datetime.getMonth()+1;        //js从0开始取
  75.  var date1 = datetime.getDate();
  76.  var hour = datetime.getHours();
  77.  var minutes = datetime.getMinutes();
  78.  var second = datetime.getSeconds();

  79.  if(month < 10){
  80.    month = "0" + month;
  81.  }
  82.  if(date1 < 10){
  83.    date1 = "0" + date1;
  84.  }
  85.  if(hour < 10){
  86.    hour = "0" + hour;
  87.  }
  88.  if(minutes <10){
  89.    minutes = "0" + minutes;
  90.  }
  91.  if(second <10){
  92.    second = "0" + second;
  93.  }

  94.  year = year.toString();
  95.  //year = year.substring(2, 4);
  96.  year = year.substr(2,2);
  97.  var time = year + month + date1 + hour + minutes + second;    //第1个变量为string,则整个变量为string????

  98.  return(time);
  99. }

  100. //dateObj是一个日期对象,days表示给这个日期加多少天,比如说4,5(天)
  101. function dateAdd(dateObj,days)
  102. {
  103.   var tempDate = dateObj.valueOf();
  104.   tempDate = tempDate + days * 24 * 60 * 60 * 1000;
  105.   tempDate = new Date(tempDate);
  106.   return tempDate;
  107. }

  108. function Trim(str){
  109.  if(str.charAt(0) == " "){
  110.   str = str.slice(1);
  111.   str = Trim(str);
  112.  }
  113.  return str;
  114. }

  115. //判断是否是空
  116. function isEmpty(pObj,errMsg){
  117.  var obj = eval(pObj);
  118.  if( obj == null || Trim(obj.value) == ""){
  119.   if (errMsg == null || errMsg =="")
  120.    alert("输入为空!");
  121.   else
  122.    alert(errMsg);
  123.   obj.focus();
  124.   return false;
  125.  }
  126.  return true;
  127. }

  128. //判断是否是数字
  129. function isNumber(pObj,errMsg){
  130.  var obj = eval(pObj);
  131.  strRef = "1234567890";
  132.  if(!isEmpty(pObj,errMsg))return false;
  133.  for (i=0;i<obj.value.length;i++) {
  134.   tempChar= obj.value.substring(i,i+1);
  135.   if (strRef.indexOf(tempChar,0)==-1) {
  136.    if (errMsg == null || errMsg =="")
  137.     alert("数据不符合要求,请检查");
  138.    else
  139.     alert(errMsg);
  140.    if(obj.type=="text")
  141.     obj.focus();
  142.    return false;
  143.   }
  144.  }
  145.  return true;
  146. }

  147. function isIE()
  148. {
  149.     if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
  150.         return true;
  151.     else
  152.         return false;
  153. }

  154. // 返回两数之间随机数的函数
  155. function rand(a, b){
  156.     return Math.round(a + Math.random()*(b - a));
  157. }

  158. // 定义名为each的函数,方便遍历数组或者对象的处理
  159. function each(o, f){
  160.     if(o.length == undefined) {
  161.         for(var n in o)             //for...in语句
  162.             f.call(o[n], n, o[n]);
  163.     }else{
  164.         for(var i=0, l = o.length; i<l; i++)
  165.             f.call(o[i], i, o[i]);
  166.     }
  167. }

  168. Array.prototype.each = function(callback)
  169. {
  170.     for (i=0; i<this.length; i++) {
  171.        callback.call(this, this[i]);
  172.     }
  173. }
  174. var arrayObject = ["JavaScript","Prototype","Jquery"];
  175. arrayObject.each(function(x){alert(x)});
  176. //用prototype方法给JavaScrip的数组对象Array加一个方法each,each方法传入一个回调函数callback,
  177. //callback内部根据当前调用数组对象长度循环执行callback方法,callback方法在执行时内部this就指当前调用,
  178. //并传入一个参数this[i]为数组当前索引对象。上面代码运行时会顺序看到弹出对话框显示"JavaScript","Prototype","Jquery"。

  179. //
  180. //转成人民币大写金额形式
  181. function RMBUpper(num)
  182. {
  183.   var strOutput = "";
  184.   var strUnit = '仟佰拾亿仟佰拾万仟佰拾元角分';
  185.   num += "00";
  186.   var intPos = num.indexOf('.');
  187.   if (intPos >= 0)
  188.     num = num.substring(0, intPos) + num.substr(intPos + 1, 2);
  189.   strUnit = strUnit.substr(strUnit.length - num.length);
  190.   for (var i=0; i < num.length; i++)
  191.     strOutput += '零壹贰叁肆伍陆柒捌玖'.substr(num.substr(i,1),1) + strUnit.substr(i,1);

  192.   return strOutput.replace(/零角零分$/, '整').replace(/[仟佰拾]/g, '零').replace(/{2,}/g, '零').replace(/([亿|])/g, '$1').replace(/+/, '元').replace(/亿零{0,3}/, '亿').replace(/^/, "零元");
  193. };

  194. function cmycurd(num)
  195. {
  196.   var str1 = '零壹贰叁肆伍陆柒捌玖'; //0-9所对应的汉字
  197.   var str2 = '万仟佰拾亿仟佰拾万仟佰拾元角分'; //数字位所对应的汉字
  198.   var str3; //从原num值中取出的值
  199.   var str4; //数字的字符串形式
  200.   var str5 = ''; //人民币大写金额形式
  201.   var i; //循环变量
  202.   var j; //num的值乘以100的字符串长度
  203.   var ch1; //数字的汉语读法
  204.   var ch2; //数字位的汉字读法
  205.   var nzero = 0; //用来计算连续的零值是几个

  206.   num = Math.abs(num).toFixed(2); //将num取绝对值并四舍五入取2位小数
  207.   str4 = (num * 100).toFixed(0).toString(); //将num乘100并转换成字符串形式
  208.   j = str4.length; //找出最高位
  209.   if (j > 15){return '溢出';}
  210.   str2 = str2.substr(15-j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分

  211.   //循环取出每一位需要转换的值
  212.   for(i=0;i<j;i++){
  213.     str3 = str4.substr(i,1); //取出需转换的某一位的值
  214.     if (i != (j-3) && i != (j-7) && i != (j-11) && i != (j-15)){ //当所取位数不为元、万、亿、万亿上的数字时
  215.     if (str3 == '0'){
  216.       ch1 = '';
  217.       ch2 = '';
  218.       nzero = nzero + 1;
  219.     }
  220.     else{
  221.       if(str3 != '0' && nzero != 0){
  222.         ch1 = '零' + str1.substr(str3*1,1);
  223.         ch2 = str2.substr(i,1);
  224.         nzero = 0;
  225.       }
  226.       else{
  227.         ch1 = str1.substr(str3*1,1);
  228.         ch2 = str2.substr(i,1);
  229.         nzero = 0;
  230.       }
  231.     }
  232.    }
  233.    else{ //该位是万亿,亿,万,元位等关键位
  234.       if (str3 != '0' && nzero != 0){
  235.         ch1 = "零" + str1.substr(str3*1,1);
  236.         ch2 = str2.substr(i,1);
  237.         nzero = 0;
  238.       }
  239.       else{
  240.      if (str3 != '0' && nzero == 0){
  241.           ch1 = str1.substr(str3*1,1);
  242.           ch2 = str2.substr(i,1);
  243.           nzero = 0;
  244.       }
  245.         else{
  246.     if (str3 == '0' && nzero >= 3){
  247.             ch1 = '';
  248.             ch2 = '';
  249.             nzero = nzero + 1;
  250.        }
  251.        else{
  252.       if (j >= 11){
  253.               ch1 = '';
  254.               nzero = nzero + 1;
  255.    }
  256.    else{
  257.      ch1 = '';
  258.      ch2 = str2.substr(i,1);
  259.      nzero = nzero + 1;
  260.    }
  261.           }
  262.   }
  263.    }
  264.  }
  265.     if (i == (j-11) || i == (j-3)){ //如果该位是亿位或元位,则必须写上
  266.         ch2 = str2.substr(i,1);
  267.     }
  268.     str5 = str5 + ch1 + ch2;

  269.     if (i == j-1 && str3 == '0' ){ //最后一位(分)为0时,加上“整”
  270.       str5 = str5 + '整';
  271.     }
  272.   }
  273.   if (num == 0){
  274.     str5 = '零元整';
  275.   }
  276.   return str5;
  277. }

2. 
3.


阅读(1648) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~