帮我制作一个计算器的页面
时间: 2024-02-20 11:55:59 浏览: 52
好的,我可以帮您制作一个基础的计算器页面,以下是一个简单的 HTML、CSS 和 JavaScript 代码示例,您可以根据自己的需要进行修改和优化:
```html
<!DOCTYPE html>
<html>
<head>
<title>计算器</title>
<style>
body {
background-color: #f0f0f0;
}
.calculator {
width: 300px;
margin: 100px auto;
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
padding: 20px;
}
.screen {
text-align: right;
font-size: 24px;
height: 40px;
margin-bottom: 10px;
padding: 5px;
}
.button {
width: 50px;
height: 50px;
background-color: #eee;
border: none;
border-radius: 5px;
margin-right: 10px;
margin-bottom: 10px;
font-size: 20px;
cursor: pointer;
}
.button:hover {
background-color: #ddd;
}
.button:active {
background-color: #ccc;
}
.clear {
background-color: #f2a8a8;
}
.equal {
background-color: #a8f2a8;
}
</style>
</head>
<body>
<div class="calculator">
<div class="screen"></div>
<button class="button clear">C</button>
<button class="button" value="7">7</button>
<button class="button" value="8">8</button>
<button class="button" value="9">9</button>
<button class="button" value="+">+</button>
<button class="button" value="4">4</button>
<button class="button" value="5">5</button>
<button class="button" value="6">6</button>
<button class="button" value="-">-</button>
<button class="button" value="1">1</button>
<button class="button" value="2">2</button>
<button class="button" value="3">3</button>
<button class="button" value="*">*</button>
<button class="button" value="0">0</button>
<button class="button" value=".">.</button>
<button class="button equal" value="=">=</button>
<button class="button" value="/">/</button>
</div>
<script>
var screen = document.querySelector(".screen");
var buttons = document.querySelectorAll(".button");
var clearButton = document.querySelector(".clear");
var equalButton = document.querySelector(".equal");
var expression = "";
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener("click", function () {
expression += this.value;
screen.textContent = expression;
});
}
clearButton.addEventListener("click", function () {
expression = "";
screen.textContent = expression;
});
equalButton.addEventListener("click", function () {
expression = eval(expression);
screen.textContent = expression;
});
</script>
</body>
</html>
```
这段代码实现了一个基础的计算器页面,包括数字按钮、运算符按钮、清空按钮和等于按钮,并且可以在屏幕上显示出计算结果。您可以根据自己的需要进行修改和优化,比如增加更多的功能和样式等。
阅读全文