利用JavaScript实现加减乘除运算

时间:2024-11-21 14:02:17
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"><html xmlns="http:///1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>实现加减乘除</title><script type="text/javascript">    function operation(){     //第一个数     var oneNumber =  parseInt(document.getElementById("one").value);  alert(oneNumber);    //第二个数  var twoNumber =  parseInt(document.getElementById("two").value);  alert(twoNumber);    //运算符  var calculate = document.getElementById("select").value;  alert(calculate);    //计算结果  var result = parseInt(document.getElementById("select").value);    /**  *计算符号的判断  */  switch(calculate)  {      case'+':result = oneNumber + twoNumber;        break;      case'-':result = oneNumber - twoNumber;        break;      case'*':result = oneNumber * twoNumber;        break;      case'/':result = oneNumber / twoNumber;       break;       default:alert("错误");       break;  } }</script></head><body>   <input type="text" id="one" size="12" style="width:40px; "/>    <select id="select">    <option value="+">+</option>    <option value="-">-</option>    <option value="*">*</option>    <option value="/">/</option> </select>   <input type="text" id="two" size="12" style="width:40px; "/> =   <input type="button" id="three" value="?" style="border:none; background-color:#FFFFFF;" onclick="operation();"/></body></html>