計算機上的退格鍵只需要做一件事:把目前顯示的字串砍掉最後一個字元。這個範例示範如何用正則表達式達成,並順帶處理小數點重複輸入的問題。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>模擬計算機</title>
<style type="text/css">
.myinput { width: 70px; height: 30px; }
.tf {
width: 220px;
height: 30px;
margin-bottom: 5px;
font-size: 26px;
font-family: Tahoma, Geneva, sans-serif;
color: #fff;
border: 2px solid #999;
background: #000;
text-align: right;
}
</style>
<script language="javascript" type="text/javascript">
window.onload = function () {
var tf = $("tf");
for (var i = 0; i < 11; i++) {
$("btn" + i).onclick = function () {
// 避免出現多個小數點
if (this.value == "." && tf.value == "") return false;
if (this.value == "." && tf.value.indexOf(".") != -1) return false;
// 避免前導 0,例如 05
if (tf.value == "0") {
if (this.value == ".") {
tf.value += this.value;
}
} else {
tf.value += this.value;
}
};
}
$("back").onclick = function () {
tf.value = tf.value.replace(/.$/, ''); // 砍掉最後一個字元
};
};
function $(id) { return document.getElementById(id); }
</script>
</head>
<body>
<input class="tf" name="textfield" type="text" id="tf" size="30" />
<br />
<input class="myinput" type="submit" name="button" id="btn0" value="0" />
<input class="myinput" type="submit" name="button" id="btn1" value="1" />
<input class="myinput" type="submit" name="button" id="btn2" value="2" />
<br />
<input class="myinput" type="submit" name="button" id="btn3" value="3" />
<input class="myinput" type="submit" name="button" id="btn4" value="4" />
<input class="myinput" type="submit" name="button" id="btn5" value="5" />
<br />
<input class="myinput" type="submit" name="button" id="btn6" value="6" />
<input class="myinput" type="submit" name="button" id="btn7" value="7" />
<input class="myinput" type="submit" name="button" id="btn8" value="8" />
<br />
<input class="myinput" type="submit" name="button" id="btn9" value="9" />
<input class="myinput" type="submit" name="button" id="btn10" value="." />
<input class="myinput" type="submit" name="button" id="back" value="退格" />
</body>
</html>
tf.value = tf.value.replace(/.$/, '');
正則 /.$/ 的意義是「任意一個字元,位於字串結尾」,replace 成空字串就等于刪掉最後一位。比 substring(0, len - 1) 更簡潔,也不必先取長度。
若想一次刪掉所有內容,直接 tf.value = '' 即可。
type="submit",在表單中會觸發送出。計算機按鈕應是 type="button"。var self = this 或直接改用 let/事件委派,可避免閉包捕獲問題。keydown 並比對 e.key。inputmode="decimal",手機鍵盤會直接跳出數字面板。
(function () {
var display = document.getElementById('tf');
var current = '0';
function render() {
display.value = current;
}
function input(ch) {
if (ch === '.') {
if (current.indexOf('.') !== -1) return; // 已有小數點則忽略
if (current === '') current = '0';
}
// 避免前導 0
if (current === '0' && ch !== '.') current = '';
if (current.replace('-', '').replace('.', '').length < 12) {
current += ch; // 限制位數
}
render();
}
function backspace() {
current = current.slice(0, -1); // 比正則更直觀
if (current === '' || current === '-') current = '0';
render();
}
function clearAll() { current = '0'; render(); }
// 事件委派:一個監聽處理所有按鈕
document.querySelector('.keypad').addEventListener('click', function (e) {
var v = e.target.value;
if (v === undefined) return;
if (v === '退格') backspace();
else if (v === 'C') clearAll();
else input(v);
});
// 實體鍵盤支援
document.addEventListener('keydown', function (e) {
if (/^[0-9.]$/.test(e.key)) input(e.key);
else if (e.key === 'Backspace') { e.preventDefault(); backspace(); }
else if (e.key === 'Escape') clearAll();
});
render();
})();
current.slice(0, -1) 表達「去掉最後一個字元」比正則更直白,效能也更好。若真的要計算運算結果,請注意 JavaScript 的浮點誤差(0.1 + 0.2 !== 0.3),實作時應以「分」為單位做整數運算,或引入 decimal.js 這類函式庫。