構建一個基於 HTML/CSS/JavaScript 的網頁計算器,實現數字按鈕點擊後將數字追加顯示在頂部輸入框,支持四則運算、清除、退位、平方等功能。
val)、首個操作數(temp)、運算符(oper)。inputEvent(this) 追加顯示inputOper(this) 暫存操作數並清空輸入框inputEquel(this) 執行計算並顯示結果inputPCB(this)Calculator 類,jisuan(num1, num2, oper) 通過 switch 執行具體運算。<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS 計算器</title>
<style>
input[type=button] { width: 60px; height: 40px; font-size: 18px; }
#display { width: 280px; height: 40px; text-align: right; font-size: 20px; padding: 5px; }
table { border-collapse: collapse; margin: 20px auto; }
td { padding: 5px; }
</style>
</head>
<body>
<table>
<tr><td colspan="4"><input id="display" type="text" readonly></td></tr>
<tr>
<td><input type="button" value="x²" onclick="handleFunc(this)"></td>
<td><input type="button" value="C" onclick="handleFunc(this)"></td>
<td><input type="button" value="<--" onclick="handleFunc(this)"></td>
<td></td>
</tr>
<tr>
<td><input type="button" value="7" onclick="inputNum(this)"></td>
<td><input type="button" value="8" onclick="inputNum(this)"></td>
<td><input type="button" value="9" onclick="inputNum(this)"></td>
<td><input type="button" value="/" onclick="inputOp(this)"></td>
</tr>
<tr>
<td><input type="button" value="4" onclick="inputNum(this)"></td>
<td><input type="button" value="5" onclick="inputNum(this)"></td>
<td><input type="button" value="6" onclick="inputNum(this)"></td>
<td><input type="button" value="*" onclick="inputOp(this)"></td>
</tr>
<tr>
<td><input type="button" value="1" onclick="inputNum(this)"></td>
<td><input type="button" value="2" onclick="inputNum(this)"></td>
<td><input type="button" value="3" onclick="inputNum(this)"></td>
<td><input type="button" value="-" onclick="inputOp(this)"></td>
</tr>
<tr>
<td><input type="button" value="0" onclick="inputNum(this)"></td>
<td><input type="button" value="." onclick="inputNum(this)"></td>
<td><input type="button" value="=" onclick="calcResult(this)"></td>
<td><input type="button" value="+" onclick="inputOp(this)"></td>
</tr>
</table>
<script>
const display = document.getElementById('display');
let firstOperand = null;
let operator = null;
let waitingForSecond = false;
function inputNum(btn) {
if (waitingForSecond) {
display.value = btn.value;
waitingForSecond = false;
} else {
display.value += btn.value;
}
}
function inputOp(btn) {
const inputVal = parseFloat(display.value);
if (firstOperand === null) {
firstOperand = inputVal;
} else if (operator) {
firstOperand = calculate(firstOperand, inputVal, operator);
display.value = firstOperand;
}
operator = btn.value;
waitingForSecond = true;
}
function calcResult() {
if (operator === null || waitingForSecond) return;
const secondOperand = parseFloat(display.value);
display.value = calculate(firstOperand, secondOperand, operator);
firstOperand = null;
operator = null;
waitingForSecond = false;
}
function handleFunc(btn) {
switch (btn.value) {
case 'C':
display.value = '';
firstOperand = null;
operator = null;
waitingForSecond = false;
break;
case '<--':
display.value = display.value.slice(0, -1);
break;
case 'x²':
const val = parseFloat(display.value);
display.value = val * val;
break;
}
}
function calculate(a, b, op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return b !== 0 ? a / b : 'Error';
default: return b;
}
}
</script>
</body>
</html>
readonly 防止鍵盤干擾,僅通過按鈕輸入。waitingForSecond 標記是否等待第二操作數,避免連續運算符覆蓋。eval() 或表達式解析器支持連續混合運算(如 1+2*3)。keydown)實現物理鍵盤操作。