JavaScript實現(xiàn)簡單的計算器功能
本文實例為大家分享了JavaScript實現(xiàn)簡單計算器功能的具體代碼,供大家參考,具體內(nèi)容如下
具體要求如下:
實現(xiàn)代碼:
<html> <head> <meta charset='utf-8'> <title>計算器</title> <script> function myck(type){var num1 = document.getElementById('num1');var num2 = document.getElementById('num2');if(type==1){ // 計算操作 var result = parseInt(num1.value) + parseInt(num2.value); alert(result); document.getElementById('resultDiv').innerText ='最終計算結(jié)果:'+ result;}else if(type==2){ var result = parseInt(num1.value) - parseInt(num2.value); alert(result); document.getElementById('resultDiv').innerText ='最終計算結(jié)果:'+ result; } else if(type==3){ var result = parseInt(num1.value) * parseInt(num2.value); alert(result); document.getElementById('resultDiv').innerText ='最終計算結(jié)果:'+ result; } else if(type==4){if(confirm('是否正確清空?')){// 清空num1.value = '';num2.value = '';document.getElementById('resultDiv').innerText=''; } } } </script> </head> <body> <div style='margin-top: 100px;margin-left: 500px;'> <span style='font-size: 60px;'>計算器</span> </div> <div> <div style='margin-left: 490px;'>數(shù) 字1:<input type='number' placeholder='請輸入數(shù)字1'> </div> </div> <div> <div style='margin-left:490px;'> 數(shù) 字2:<input type='number' placeholder='請輸入數(shù)字2'> </div> </div> <div> <div class='innerDiv'><input type='button' onclick='myck(1)' value='相 加'> <input type='button' onclick='myck(2)' value='相 減'> <input type='button' onclick='myck(3)' value='相 乘'><input type='button' onclick='myck(4)' value='清 空'> </div> </div> <div id='resultDiv'> </div> </body> <style> .innerDiv{ margin-left: 420px; margin-top: 20px; } </style></html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 一款功能強大的markdown編輯器tui.editor使用示例詳解2. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程3. 利用CSS制作3D動畫4. SpringBoot+TestNG單元測試的實現(xiàn)5. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進)6. Python+unittest+requests 接口自動化測試框架搭建教程7. Java GZip 基于內(nèi)存實現(xiàn)壓縮和解壓的方法8. PHP利用COM對象訪問SQLServer、Access9. 存儲于xml中需要的HTML轉(zhuǎn)義代碼10. Springboot 全局日期格式化處理的實現(xiàn)
