JavaScript版簡易計算機

來源:互聯網
上載者:User

在JavaScript版搜尋了一下“計算機”,只搜尋到三條記錄。所以發出來,今天沒事寫著玩兒的。比較簡單,沒有什麼技術含量和實用價值,只供新手學習,高手看看有沒問題,再完善一下。

測試環境:IE6 + FireFox1.5下測試通過

技術說明:
1、documnet.getElementsByTagName的用法
2、給HTML對象綁定事件:element.事件名 = 函數名
3、eval的用法

源碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>簡易計算機-JavaScript版</title>
</head>
<style type="text/css">
<!--
body td {
 font-size: 12px;
}
table {
 border: 2px outset #F5F5F5;
 background-color: #D6D3CE;
}
input {
 font-family: Arial, Helvetica, sans-serif;
 font-size: 12px;
 width: 24px;
 height: 24px;
 color: #0000FF;
}
.r {
 width: 120px;
 height: auto;
 background-color: #F5F5F5;
 color: #000000;
}
.b {
 width: 80px;
 color: #FF0000;
}
.color_red {
 color: #FF0000;
}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
window.onload = function() {
  var elements = document.getElementsByTagName("input");
  for(i = 0; i < elements.length; i++){
    if (elements[i].type == "button"){
      switch (elements[i].id){
        case "b": elements[i].onclick = backSpace; break;
        case "c": elements[i].onclick = clear; break;
        case "=": elements[i].onclick = calculate; break;
        default : elements[i].onclick = oper; break;
      }
    }
  }
}

function backSpace() {
  var value = this.form.r.value;
  this.form.r.value = value.substr(0, value.length - 1);
}

function clear(){
  this.form.r.value = "";
}

function calculate(){
  try{
    var result = eval(this.form.r.value);
    if (isNaN(result)){
      this.form.r.value = "無法計算";
    }else if (!isFinite(result)){
      this.form.r.value = "無意義";
    } else {
      this.form.r.value = result;
    }
  } catch (e){
    this.form.r.value = "發生錯誤";
  }
}

function oper(){
  this.form.r.value += this.value;
}
//-->
</script>
<body>
<form id="form1" name="form1" method="post" action="">
  <table width="100" border="0" align="center" cellpadding="3" cellspacing="1">
    <tr>
      <td colspan="4"><input name="r" type="text" class="r" id="r" size="16" maxlength="16" readonly="readonly"/></td>
    </tr>
    <tr>
      <td colspan="3" align="right"><input name="b" type="button" class="b" id="b" value="BackSpace"/></td>
      <td><input name="c" type="button" class="color_red" id="c" value="C" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input name="(" type="button" class="color_red" id="(" value="(" /></td>
      <td><input name=")" type="button" class="color_red" id=")" value=")" /></td>
    </tr>
    <tr>
      <td><input name="7" type="button" id="7" value="7" /></td>
      <td><input name="8" type="button" id="8" value="8" /></td>
      <td><input name="9" type="button" id="9" value="9" /></td>
      <td><input name="/" type="button" class="color_red" id="/" value="/" /></td>
    </tr>
    <tr>
      <td><input name="4" type="button" id="4" value="4" /></td>
      <td><input name="5" type="button" id="5" value="5" /></td>
      <td><input name="6" type="button" id="6" value="6" /></td>
      <td><input name="*" type="button" class="color_red" id="*" value="*" /></td>
    </tr>
    <tr>
      <td><input name="1" type="button" id="1" value="1" /></td>
      <td><input name="2" type="button" id="2" value="2" /></td>
      <td><input name="3" type="button" id="3" value="3" /></td>
      <td><input name="-" type="button" class="color_red" id="-" value="-" /></td>
    </tr>
    <tr>
      <td><input name="0" type="button" id="0" value="0" /></td>
      <td><input name="." type="button" id="." value="." /></td>
      <td><input name="=" type="button" class="color_red" id="=" value="=" /></td>
      <td><input name="1+" type="button" class="color_red" id="1+" value="+" /></td>
    </tr>
  </table>
</form>
</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.