當前位置:編程學習大全網 - 編程軟體 - 簡易的加減乘除的計算器代碼js

簡易的加減乘除的計算器代碼js

//html

<input?type="text"?id="num1"?value=""?/>

<select?id="mySelect">

<option?value="+">+</option>

<option?value="-">-</option>

<option?value="*">*</option>

<option?value="/">/</option>

</select>

<input?type="text"?id="num2"?value=""?/>

<input?type="button"?id="jisuan"?value="計算"?/>

//js

<script>

var?oTxt1?=?document.getElementById('num1');

var?oTxt2?=?document.getElementById('num2');

var?oSelect?=?document.getElementById('mySelect');

var?oBtn?=?document.getElementById('jisuan');

oBtn.onclick=function(){

switch(oSelect.value){

case?'+':

alert(parseInt(oTxt1.value)+parseInt(oTxt2.value));

break;

case?'-':

alert(parseInt(oTxt1.value)-parseInt(oTxt2.value));

break;

case?'*':

alert(parseInt(oTxt1.value)*parseInt(oTxt2.value));

break;

case?'/':

if(parseInt(oTxt2.value)?!==?0){

alert(parseInt(oTxt1.value)/parseInt(oTxt2.value));

}else{

alert('除數不能為0');

}

break;

default:

alert('Bug!!!');

}

}

</script>

  • 上一篇:做編程需要什麽電腦配置
  • 下一篇:C語言中float是什麽意思
  • copyright 2024編程學習大全網