请帮我写个js 的计算机
时间: 2023-05-25 18:03:14 浏览: 120
以下是一个简单的 JavaScript 计算机:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>简单计算器</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
margin-top: 50px;
}
.container {
width: 400px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="button"] {
display: block;
width: 100%;
padding: 10px;
margin-top: 10px;
background-color: #008CBA;
border: none;
color: #fff;
font-size: 16px;
text-align: center;
cursor: pointer;
border-radius: 5px;
}
input[type="text"] {
display: block;
width: 100%;
padding: 10px;
margin-top: 10px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>简单计算器</h1>
<div class="container">
<input type="text" id="result" placeholder="结果">
<input type="button" id="clear" value="清除">
<input type="button" id="backspace" value="退格">
<input type="button" id="divide" value="/">
<input type="button" id="multiply" value="*">
<input type="button" id="seven" value="7">
<input type="button" id="eight" value="8">
<input type="button" id="nine" value="9">
<input type="button" id="subtract" value="-">
<input type="button" id="four" value="4">
<input type="button" id="five" value="5">
<input type="button" id="six" value="6">
<input type="button" id="add" value="+">
<input type="button" id="one" value="1">
<input type="button" id="two" value="2">
<input type="button" id="three" value="3">
<input type="button" id="equals" value="=">
<input type="button" id="zero" value="0">
<input type="button" id="decimal" value=".">
</div>
<!-- JavaScript 文件 -->
<script src="calculator.js"></script>
</body>
</html>
```
JavaScript:
```javascript
// 获取所有按钮
var clearButton = document.getElementById("clear");
var backspaceButton = document.getElementById("backspace");
var divideButton = document.getElementById("divide");
var multiplyButton = document.getElementById("multiply");
var sevenButton = document.getElementById("seven");
var eightButton = document.getElementById("eight");
var nineButton = document.getElementById("nine");
var subtractButton = document.getElementById("subtract");
var fourButton = document.getElementById("four");
var fiveButton = document.getElementById("five");
var sixButton = document.getElementById("six");
var addButton = document.getElementById("add");
var oneButton = document.getElementById("one");
var twoButton = document.getElementById("two");
var threeButton = document.getElementById("three");
var equalsButton = document.getElementById("equals");
var zeroButton = document.getElementById("zero");
var decimalButton = document.getElementById("decimal");
// 获取结果文本框
var result = document.getElementById("result");
// 点击事件添加
clearButton.addEventListener("click", function() {
result.value = "";
});
backspaceButton.addEventListener("click", function() {
result.value = result.value.slice(0, -1);
});
divideButton.addEventListener("click", function() {
result.value += "/";
});
multiplyButton.addEventListener("click", function() {
result.value += "*";
});
sevenButton.addEventListener("click", function() {
result.value += "7";
});
eightButton.addEventListener("click", function() {
result.value += "8";
});
nineButton.addEventListener("click", function() {
result.value += "9";
});
subtractButton.addEventListener("click", function() {
result.value += "-";
});
fourButton.addEventListener("click", function() {
result.value += "4";
});
fiveButton.addEventListener("click", function() {
result.value += "5";
});
sixButton.addEventListener("click", function() {
result.value += "6";
});
addButton.addEventListener("click", function() {
result.value += "+";
});
oneButton.addEventListener("click", function() {
result.value += "1";
});
twoButton.addEventListener("click", function() {
result.value += "2";
});
threeButton.addEventListener("click", function() {
result.value += "3";
});
equalsButton.addEventListener("click", function() {
result.value = eval(result.value);
});
zeroButton.addEventListener("click", function() {
result.value += "0";
});
decimalButton.addEventListener("click", function() {
result.value += ".";
});
```
这个 JavaScript 计算器接受用户的键输入,并在结果文本框中显示相应的数字和运算符。用户还可以使用退格和清除按钮来修改、重置输入。在用户完成输入后,可以单击等号按钮来计算结果,结果将在结果文本框中显示。
阅读全文