javascript - 单击按钮时无法更改输入框的值
问题描述
嘿伙计们,我正在尝试制作一个简单的 JavaScript 计算器。现在我只是想更改输入框以显示我单击的按钮,但我无法弄清楚它为什么不起作用。这是我的代码。
var display = document.getElementById('display');
function toScreen(x) {
display ='';
display.textContent = x;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Javascript Calculator</h1>
<div id="calculator">
<form>
<input type="text" id="display" disabled>
<br>
<input type="button" value="C" id="keys" onclick="toScreen('c')">
<input type="button" value="CE" id="keys" onclick="clearEntry('c')">
<input type="button" value="x^2" id="keys" onclick="powerOf('c')">
<input type="button" value="+" id="keys" onclick="toScreen('+')">
<br>
<input type="button" value="9" id="keys" onclick='toScreen("9")'>
<input type="button" value="8" id="keys" onclick="toScreen('8')">
<input type="button" value="7" id="keys" onclick="toScreen('7')">
<input type="button" value="-" id="keys" onclick="toScreen('-')">
<br>
<input type="button" value="6" id="keys" onclick="toScreen('6')">
<input type="button" value="5" id="keys" onclick="toScreen('5')">
<input type="button" value="4" id="keys" onclick="toScreen('4')">
<input type="button" value="x" id="keys" onclick="toScreen('x')">
<br>
<input type="button" value="3" id="keys" onclick="toScreen('3')">
<input type="button" value="2" id="keys" onclick="toScreen('2')">
<input type="button" value="1" id="keys" onclick="toScreen('1')">
<input type="button" value="/" id="keys" onclick="toScreen('/')">
<br>
<input type="button" value="0" id="keys" onclick="toScreen('0')">
<input type="button" value="." id="keys" onclick="toScreen('.')">
<input type="button" value="=" id="equal" onclick="equal()">
</form>
<h3>Javascript Calculator</h3>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
解决方案
我在这里附上了工作代码。如果您只想替换输入中的文本,请+
从函数中删除。
var display = document.getElementById('display');
function toScreen(x) {
display.value += x;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>calculator</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Javascript Calculator</h1>
<div id="calculator">
<form>
<input type="text" id="display" disabled>
<br>
<input type="button" value="C" id="keys" onclick="toScreen('c')">
<input type="button" value="CE" id="keys" onclick="clearEntry('c')">
<input type="button" value="x^2" id="keys" onclick="powerOf('c')">
<input type="button" value="+" id="keys" onclick="toScreen('+')">
<br>
<input type="button" value="9" id="keys" onclick='toScreen("9")'>
<input type="button" value="8" id="keys" onclick="toScreen('8')">
<input type="button" value="7" id="keys" onclick="toScreen('7')">
<input type="button" value="-" id="keys" onclick="toScreen('-')">
<br>
<input type="button" value="6" id="keys" onclick="toScreen('6')">
<input type="button" value="5" id="keys" onclick="toScreen('5')">
<input type="button" value="4" id="keys" onclick="toScreen('4')">
<input type="button" value="x" id="keys" onclick="toScreen('x')">
<br>
<input type="button" value="3" id="keys" onclick="toScreen('3')">
<input type="button" value="2" id="keys" onclick="toScreen('2')">
<input type="button" value="1" id="keys" onclick="toScreen('1')">
<input type="button" value="/" id="keys" onclick="toScreen('/')">
<br>
<input type="button" value="0" id="keys" onclick="toScreen('0')">
<input type="button" value="." id="keys" onclick="toScreen('.')">
<input type="button" value="=" id="equal" onclick="equal()">
</form>
<h3>Javascript Calculator</h3>
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
推荐阅读
- r - 如果 R 中不存在,如何用 NULL 值填充 DF 列表
- python - 如何将多个熊猫数据框中的行提取到一个变量中?
- flutter - Flutter 传递图像字符串
- bash - 防止 zsh 将文字引号添加到从 bash 脚本运行的命令
- python - 我可以从 django 中的另一个视图函数发送 POST 请求来查看函数吗?
- php - 命名参数后不能使用位置参数 | Yii2
- html - 自动垂直对齐框
- node.js - selenium-webdriver:在当前 PATH 上找不到 geckodriver 可执行文件
- python - PyInstaller 从 .py 转换为 .exe 时无法执行脚本
- reactjs - 在父组件的 DOM 层次结构之外插入组件的新实例 onClick