1 <script type="text/javascript">
2 function fnSum(num) {
3 var sum = 0;
4 while(num != 0) {
5 sum += num % 10; //先取出个位数;
6 num = parseInt(num / 10);//再去除每个 个位数。
7 }
8 return sum;
9 }
10 var s = fnSum(123);
11 alert(s);
12 </script>