自己写的js给数字豆号分节,来提高自己
转自:http://www.ok22.org/art_detail.aspx?id=56 (可直接运行)
- < html >
- < body >
- < table border = "1" cellspacing = "5" id = "sp" >
- < tr > < td > 12345436267.89 </ td > < td > 3402.89 </ td > < td > 3402 </ td > </ tr > </ table >
- < script >
- function formatNumber(num){
- if(!/^(\+|-)?(\d+)(\.\d+)?$/.test(num)){return num;}
- var a = RegExp .$1, b = RegExp .$2, c = RegExp .$3;
- var re = new RegExp().compile("(\\d)(\\d{3})(,|$)");
- while(re.test(b)) b b = b.replace(re,"$1,$2$3");
- return a +""+ b +""+ c;
- }
- function spl(){
- var tdvalue = document .getElementById("sp").getElementsByTagName("td");
- var std = "" ;
- var rstr = "" ;
- for(j = 0 ;j < tdvalue.length ;j++)
- { std = "" ;
- std = tdvalue [j];
- rstr = formatNumber (std.innerHTML);
- std.innerHTML = rstr
- }
- }
- spl();
- </ script >
- </ body >
- </ html >