javascript簡單計算機

來源:互聯網
上載者:User
提示:您可以先修改部分代碼再運行

<!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=utf-8" /> <title>寫給新手:js簡單計算機</title> <style type="text/css"> body{ font-size:12px; color:#333; } #jsq input{/*輸入框樣式*/ border:#ccc 1px solid; border-right:#e2e2e2 1px solid; border-bottom:#e2e2e2 1px solid; height:18px; line-height:18px; padding:3px; } #jsq span{ color:#999 } #jsq input.btn{/*按鈕樣式*/ border:#e6e6e6 1px solid; background-color:#e2e2e2; width:30px; height:24px; text-align:center; line-height:16px; cursor:pointer; margin:0 3px; color:#999; } #jsq input.btn:hover{/*按鈕懸浮樣式*/ border:#e2e2e2 1px solid; background-color:#f2f2f2; color:#333; } </style> <script type="text/javascript"> function imyeah(type){ //計算函數 var result=0; num1 = Number(document.jisuanqi.num1.value); //Number()可以吧字串強制轉換成數字,例如“123abc”會轉換成“123” num2 = Number(document.jisuanqi.num2.value); if(num1=="" || num2==""){return false;} //如果沒輸入計算數則不執行 switch(type){ //判斷要執行的計算符號 case 0:result=num1+num2;break; //計算“+” case 1:result=num1-num2;break; //計算“-” case 2:result=num1*num2;break; case 3:result=num1/num2;break; case 4:result=num1%num2;break; } document.jisuanqi.jieguo.value=result; //顯示計算結果 } </script> </head> <body> <form name="jisuanqi" id="jsq" action="" method="get" /> <p> 第一個數: <input type="text" size="10" name="num1" value="" /> </p> <p> 第二個數: <input type="text" size="10" name="num2" value="" /> </p> <p> 計算結果: <input type="text" size="10" name="jieguo" onClick="imyeah(0)" value="+" onfocus="this.select()" /> <span>左鍵"+",右鍵"選中複製"</span> </p> <p> <input type="button" class="btn" value="?" onClick="imyeah(1)"/> <!--定義按鈕--> <input type="button" class="btn" value="×" onClick="imyeah(2)"/ > <input type="button" class="btn" value="÷" onClick="imyeah(3)"/> <input type="button" class="btn" value="%" onClick="imyeah(4)"/> </p> </body> </html> </td> </tr></table>
提示:您可以先修改部分代碼再運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.