编写一个html计算器的代码
时间: 2023-06-09 16:02:44 浏览: 81
用js编写的简单的计算器代码程序
好的,以下是一个简单的 HTML 计算器代码,您可以在浏览器中直接运行它:
```
<!DOCTYPE html>
<html>
<head>
<title>计算器</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
background-color: #f1f1f1;
font-family: Arial, sans-serif;
font-size: 14px;
}
.calculator {
width: 300px;
background-color: #fff;
margin: 50px auto;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0,0,0,0.2);
padding: 20px;
box-sizing: border-box;
}
.display {
height: 50px;
border: none;
border-radius: 5px;
background-color: #eee;
font-size: 20px;
text-align: right;
padding-right: 10px;
margin-bottom: 10px;
}
.button {
height: 50px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: #fff;
font-size: 18px;
cursor: pointer;
margin-right: 10px;
margin-bottom: 10px;
padding: 0 10px;
box-sizing: border-box;
}
.button:hover {
background-color: #3e8e41;
}
.button.clear {
background-color: #f44336;
}
.button.clear:hover {
background-color: #d32f2f;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" class="display" id="display" readonly>
<button class="button" onclick="append('7')">7</button>
<button class="button" onclick="append('8')">8</button>
<button class="button" onclick="append('9')">9</button>
<button class="button" onclick="append('+')">+</button>
<br>
<button class="button" onclick="append('4')">4</button>
<button class="button" onclick="append('5')">5</button>
<button class="button" onclick="append('6')">6</button>
<button class="button" onclick="append('-')">-</button>
<br>
<button class="button" onclick="append('1')">1</button>
<button class="button" onclick="append('2')">2</button>
<button class="button" onclick="append('3')">3</button>
<button class="button" onclick="append('*')">*</button>
<br>
<button class="button" onclick="append('0')">0</button>
<button class="button" onclick="append('.')">.</button>
<button class="button" onclick="calculate()">=</button>
<button class="button" onclick="append('/')">/</button>
<br>
<button class="button clear" onclick="clear()">清除</button>
</div>
<script type="text/javascript">
function append(value) {
document.getElementById('display').value += value;
}
function clear() {
document.getElementById('display').value = '';
}
function calculate() {
document.getElementById('display').value = eval(document.getElementById('display').value);
}
</script>
</body>
</html>
```
希望能帮到您。如果您有其他问题,请继续提出。
阅读全文