用JS 写个非常简单的计算器 算是学习JS的一个开头

来源:互联网 发布:数据库开发规范 编辑:程序博客网 时间:2024/05/21 10:03
<!DOCTYPE html><html> <head>  <title> 事件</title>    <script type="text/javascript">   function count(){      var a = parseInt(document.getElementById("txt1").value);      var b = parseInt(document.getElementById("txt2").value);      var c = document.getElementById("select").value;      //alert(a+" "+b+" "+c);      var s = 0;      if(c == '+'){        s = a + b;      }      else if(c == '-'){        s = a - b;      }      else if(c == '*'){        s = a * b;      }      else{        if(b == 0) s = "除数不能为0";        else s = a / b;      }      document.getElementById("fruit").value = s;   }  </script>  </head>  <body>   <input type='text' id='txt1' />    <select id='select'>    <option value='+'>+</option>    <option value="-">-</option>    <option value="*">*</option>    <option value="/">/</option>   </select>   <input type='text' id='txt2' />    <input type='button' value=' = ' onClick = "count()" /> <!--通过 = 按钮来调用创建的函数,得到结果-->    <input type='text' id='fruit' />    </body></html>

0 0