html做一个网页版计算器
时间: 2023-07-18 12:21:15 浏览: 38
好的,下面是一个简单的网页版计算器的示例代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>网页版计算器</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #f2f2f2;
font-family: Arial, sans-serif;
font-size: 16px;
}
.calculator {
width: 300px;
margin: 50px auto;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
padding: 20px;
}
.calculator h2 {
text-align: center;
}
.calculator input[type="button"] {
width: 64px;
height: 64px;
font-size: 24px;
background-color: #eee;
border: none;
border-radius: 5px;
margin: 5px;
cursor: pointer;
}
.calculator input[type="text"] {
width: 100%;
height: 64px;
font-size: 24px;
text-align: right;
border: none;
border-radius: 5px;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div class="calculator">
<h2>计算器</h2>
<input type="text" id="result" readonly>
<input type="button" value="1" onclick="addToInput('1')">
<input type="button" value="2" onclick="addToInput('2')">
<input type="button" value="3" onclick="addToInput('3')">
<input type="button" value="+" onclick="addToInput('+')">
<br>
<input type="button" value="4" onclick="addToInput('4')">
<input type="button" value="5" onclick="addToInput('5')">
<input type="button" value="6" onclick="addToInput('6')">
<input type="button" value="-" onclick="addToInput('-')">
<br>
<input type="button" value="7" onclick="addToInput('7')">
<input type="button" value="8" onclick="addToInput('8')">
<input type="button" value="9" onclick="addToInput('9')">
<input type="button" value="*" onclick="addToInput('*')">
<br>
<input type="button" value="C" onclick="clearInput()">
<input type="button" value="0" onclick="addToInput('0')">
<input type="button" value="=" onclick="calculate()">
<input type="button" value="/" onclick="addToInput('/')">
</div>
<script>
function addToInput(value) {
document.getElementById("result").value += value;
}
function clearInput() {
document.getElementById("result").value = "";
}
function calculate() {
var input = document.getElementById("result").value;
var result = eval(input);
document.getElementById("result").value = result;
}
</script>
</body>
</html>
```
这段代码会生成一个样式简洁的计算器,支持基本的加、减、乘、除运算。你可以在浏览器中打开这个 HTML 文件,就可以看到这个计算器了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)