该部分计算器设置借鉴于网络 本人对部分代码进行修改优化 仅供参考

  1. function $(id){return document.getElementById(id);}
  2. var str;
  3. var dengyu;
  4. function init(){
  5. str = document.getElementById("text");
  6. str.value=0;
  7. str.disabled="disabled";
  8. }
  9. function isNull(n){
  10. if(n=='0'||n.length==0){
  11. return true;
  12. }else {
  13. return false;
  14. }
  15. }
  16. function display(n) //显示到文本框
  17. {
  18. str = document.getElementById("text");
  19. if (isNull(str.value)) {
  20. str.value=n;
  21. }else{
  22. str.value = str.value + n;
  23. }
  24. }
  25. function equals() //等于
  26. {
  27. str = document.getElementById("text");
  28. dengyu = eval(str.value);//Eval 函数计算表达式,并返回结果。参数expression必须需要能够计算的表达式
  29. str.value = dengyu;//此函数将在VBScript内学习 方式借鉴于网络素材
  30. }
  31. function back() //退格
  32. {
  33. str = document.getElementById("text");
  34. str.value = str.value.substr(0,str.value.length-1);
  35. if (isNull(str.value)) {
  36. str.value=0
  37. }
  38. return str.value;
  39. }
  40. function reset() //清除
  41. {
  42. str = document.getElementById("text");
  43. str.value = 0;
  44. }