JavaScript實現(xiàn)簡單的計算器功能
更新時間:2021年03月31日 08:48:39 作者:小小小青臺
這篇文章主要為大家詳細介紹了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 class="innerDiv" style="margin-left: 490px;">
數(shù) 字1:<input id="num1" type="number" placeholder="請輸入數(shù)字1">
</div>
</div>
<div>
<div class="innerDiv" style="margin-left:490px;">
數(shù) 字2:<input id="num2" type="number" placeholder="請輸入數(shù)字2">
</div>
</div>
<div>
<div style="margin-left: 500px;" 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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript調(diào)用模式與this關(guān)鍵字綁定的關(guān)系
這篇文章主要介紹了JavaScript調(diào)用模式與this關(guān)鍵字綁定的關(guān)系 的相關(guān)資料,需要的朋友可以參考下2018-04-04
在小程序中集成redux/immutable/thunk第三方庫的方法
這篇文章主要介紹了在小程序中集成redux/immutable/thunk第三方庫的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
屏蔽網(wǎng)頁右鍵復(fù)制和ctrl+c復(fù)制的js代碼
解決的方法就是直接把網(wǎng)頁保存下來然后刪掉下面這段js代碼,然后就可以正常用右鍵菜單,也可以通過設(shè)置瀏覽器的安全級別到最高級別來解決問題2013-01-01
Javascript 實現(xiàn) Excel 導(dǎo)入生成圖表功能
這篇文章主要介紹了Javascript 實現(xiàn) Excel 導(dǎo)入生成圖表功能,本文通過實例代碼講解給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10

