蜗牛—JavaScript学习之简易计算器

来源:互联网 发布:中国外来物种数据库 编辑:程序博客网 时间:2024/05/17 01:40

主要代码如下:

<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>简易计算器</title><script>function calNum(opt){var retObj = document.getElementById("txtresult");var num1 = parseInt(document.getElementById("txtNum1").value);var num2 = parseInt(document.getElementById("txtNum2").value);var ret = 0;switch(opt){case '+':ret=num1+num2;break;case '-':ret=num1-num2;break;case '*':ret=num1*num2;break;case '/':ret=num1/num2;break;}retObj.value=ret;}</script></head><body> <form action="" method="post"  id="myform"><table border="0" >  <tr>    <td><img src="images/shop.gif" width="54" height="54"></td><td colspan="3"><H3>购物简易计算器</H3></td>  </tr>  <tr>    <td>第一个数</td>    <td colspan="3"><INPUT name="txtNum1" type="text"  id="txtNum1" size="25"></td>  </tr>  <tr  >    <td>第二个数</td>    <td colspan="3"><INPUT name="txtNum2" type="text"  id="txtNum2" size="25"></td>  </tr>  <tr>    <td><INPUT name="addButton2" type="button" id="addButton2" value="  +  "     onclick="calNum('+');"></td>    <td><INPUT name="subButton2" type="button" id="subButton2" value="  -  " onclick="calNum('-')"  ></td>    <td><INPUT name="mulButton2" type="button" id="mulButton2" value="  ×  "  onclick="calNum('*')"></td>    <td><INPUT name="divButton2" type="button" id="divButton2" value="  ÷  " onclick="calNum('/')" ></td>  </tr>  <tr>    <td>计算结果</td>    <td colspan="3"><INPUT name="txtresult" type="text"  id="txtresult" size="25"></td>  </tr></table></form></body></html>


0 0
原创粉丝点击