标签:
1 <!doctype html> 2 <html> 3 <head> 4 <title>xxx</title> 5 </head> 6 <body> 7 <input id="test" type="text"> 8 <div id="display"></div> 9 <script> 10 //支持到1~9999 11 function int2zh(arg_num){ 12 var num = arg_num; 13 var arr1 = [‘零‘,‘一‘,‘二‘,‘三‘,‘四‘,‘五‘,‘六‘,‘七‘,‘八‘,‘九‘], arr2 = [‘‘, ‘十‘, ‘百‘, ‘千‘, ‘万‘]; 14 var i = 0, digit, result = []; 15 while(num){ 16 digit = num % 10; 17 if(digit === 0){ 18 if(result.length === 0) { 19 num = Math.floor(num / 10); 20 i++; 21 continue; 22 } 23 if(‘零‘ == result[result.length - 1]) { 24 num = Math.floor(num / 10); 25 i++; 26 continue; 27 } 28 result.push(‘零‘); 29 }else{ 30 result.push(arr2[i]); 31 result.push(arr1[digit]); 32 } 33 num = Math.floor(num / 10); 34 i++; 35 } 36 if(arg_num < 20 && arg_num >= 10){ 37 result.splice(result.length - 1, 1); 38 } 39 return result.reverse().join(""); 40 } 41 document.getElementById(‘test‘).addEventListener("input", function(){ 42 var val = this.value; 43 var r = int2zh(val); 44 display.innerHTML = r; 45 },false); 46 </script> 47 </body> 48 </html>
标签:
原文地址:http://www.cnblogs.com/ward/p/5161231.html