用js實現簡易計算機

來源:互聯網
上載者:User

//用js實現簡易計算機完整代碼,大家看下,不足的地方給下建議
 <!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>
</head>
<body>
<table width="33%" height="193" border="1">
  <tr>
    <td colspan="4"><input type="text" size="50" name="result" id="result"></td>
  </tr>
  <tr>
    <td width="24%"><input type="button" value="     1     "  onclick="getNum(1)"></td>
    <td width="23%"><input type="button" value="     2     " onclick="getNum(2)"></td>
    <td width="26%"><input type="button" value="     3     " onclick="getNum(3)"></td>
    <td width="27%"><input type="button" value="     +     " onclick="getNum('+')"></td>
  </tr>
  <tr>
    <td><input type="button" value="     4     " onclick="getNum(4)"></td>
    <td><input type="button" value="     5     " onclick="getNum(5)"></td>
    <td><input type="button" value="     6     " onclick="getNum(6)"></td>
    <td><input type="button" value="     -     " onclick="getNum('-')"></td>
  </tr>
  <tr>
    <td><input type="button" value="     7     " onclick="getNum(7)"></td>
    <td><input type="button" value="     8     " onclick="getNum(8)"></td>
    <td><input type="button" value="     9     " onclick="getNum(9)"></td>
    <td><input type="button" value="     *     " onclick="getNum('*')"></td>
  </tr>
  <tr>
    <td><input type="button" value="     0     " onclick="getNum(0)"></td>
    <td><input type="button" value="     .     " onclick="getNum('.')"></td>
    <td><input type="button" value="     =     " onclick="getResult()"></td>
    <td><input type="button" value="     /     " onclick="getNum('/')"></td>
  </tr>
</table>
<script type="text/javascript" language="javascript">
//判斷是否清空
var clearContent=false;
//獲得數字
function getNum(num){
if(clearContent){
//如果已經有結果存在則將其清空
document.getElementById("result").value="";
clearContent=false;
}
//擷取結果對象
var objresult=document.getElementById("result");
//實現數字串連
objresult.value+=num;
}
//擷取結果對象值
function getResult(){
var objresult2=document.getElementById("result");
//為結果賦值
objresult2.value=objresult2.value+"="+eval(objresult2.value);
clearContent=true;
}
</script>
</body>
</html>

摘自 席偉娜的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.