HTML 代码:
<input type="text" id="input-box">
<span id="result"></span>
JavaScript 代码:
const inputBox = document.getElementById('input-box');
const result = document.getElementById('result');
// 监听输入框输入事件
inputBox.addEventListener('input', function() {
const value = inputBox.value.trim();
if (value === '') {
result.textContent = '';
} else {
try {
const res = eval(value);
result.textContent = `= ${res}`;
} catch (e) {
result.textContent = '';
}
}
});