国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

JavaScript實現簡單的計算器功能

瀏覽:92日期:2023-06-07 08:34:53

本文實例為大家分享了JavaScript實現簡單計算器功能的具體代碼,供大家參考,具體內容如下

具體要求如下:

JavaScript實現簡單的計算器功能

實現代碼:

<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 ='最終計算結果:'+ result;}else if(type==2){ var result = parseInt(num1.value) - parseInt(num2.value); alert(result); document.getElementById('resultDiv').innerText ='最終計算結果:'+ result; } else if(type==3){ var result = parseInt(num1.value) * parseInt(num2.value); alert(result); document.getElementById('resultDiv').innerText ='最終計算結果:'+ 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;'>數&nbsp;&nbsp;字1:<input type='number' placeholder='請輸入數字1'> </div> </div> <div> <div style='margin-left:490px;'> 數&nbsp;&nbsp;字2:<input type='number' placeholder='請輸入數字2'> </div> </div> <div> <div class='innerDiv'><input type='button' onclick='myck(1)' value='相&nbsp;&nbsp;加'> <input type='button' onclick='myck(2)' value='相&nbsp;&nbsp;減'> <input type='button' onclick='myck(3)' value='相&nbsp;&nbsp;乘'><input type='button' onclick='myck(4)' value='清&nbsp;&nbsp;空'> </div> </div> <div id='resultDiv'> </div> </body> <style> .innerDiv{ margin-left: 420px; margin-top: 20px; } </style></html>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: JavaScript
相關文章:
主站蜘蛛池模板: 曲阜市| 定陶县| 高邑县| 奉化市| 沅陵县| 四川省| 大宁县| 汽车| 且末县| 四川省| 温州市| 永仁县| 泉州市| 行唐县| 嵊泗县| 昌吉市| 嘉荫县| 都兰县| 望江县| 朝阳市| 肇州县| 馆陶县| 阿克陶县| 陈巴尔虎旗| 丹凤县| 临猗县| 朝阳区| 万州区| 慈溪市| 金川县| 那曲县| 岳西县| 新乡县| 清镇市| 盐津县| 廉江市| 房产| 镇远县| 天门市| 垫江县| 林口县|