JavaScript计算器

it2024-12-12  17

JavaScript简单计算器代码:

<input type="text" id="number1" placeholder="请输入第一个数 字"/><br/> <input type="text" id="opt" placeholder="请输入操作符"/><br/> <input type="text" id="number2" placeholder="请输入第二个数字"/><br/> <input type="button" value="计算" onclick="jisuan()" /><br/> <span id="resultshow"></span> function jisuan(){ var number1 = document.getElementById("number1").value; var opt = document.getElementById("opt").value; var number2 = document.getElementById("number2").value; if(isNaN(menubar1) || isNaN(number2)){ alert("请输入数字"); return ; } number1 = Number(number1); number2 = Number(number2) var result = 0; switch (opt){ case "+": result = number1 + number2; break; case "-": result = number1 - number2; break; case "*": result = number1 * number2; break; case "/": if (number2==0){ alert("除数不能为0") }else{ result = number1 / number2; } break; default: result = 0; } result = result.toFixed(2); document.getElementById("resultshow").innerText=result; }
最新回复(0)