html+js+css功能齐全的计算器
时间: 2023-07-25 09:37:28 浏览: 100
jisuanqi.rar_html js计算器_js计算器_计算器_计算器 js_计算器JS
以下是一个简单的 HTML+JS+CSS 计算器的示例代码,可以进行加、减、乘、除、取模和清除操作。
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>计算器</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="calculator">
<div class="display"></div>
<button class="operator" value="+">+</button>
<button class="operator" value="-">-</button>
<button class="operator" value="*">×</button>
<button class="operator" value="/">÷</button>
<button class="operator" value="%">%</button>
<button class="number" value="7">7</button>
<button class="number" value="8">8</button>
<button class="number" value="9">9</button>
<button class="number" value="4">4</button>
<button class="number" value="5">5</button>
<button class="number" value="6">6</button>
<button class="number" value="1">1</button>
<button class="number" value="2">2</button>
<button class="number" value="3">3</button>
<button class="number" value="0">0</button>
<button id="clear">C</button>
<button id="equal">=</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
.calculator {
width: 240px;
border: 1px solid #ccc;
border-radius: 5px;
margin: 50px auto;
padding: 10px;
text-align: center;
}
.display {
font-size: 24px;
height: 40px;
margin-bottom: 10px;
padding: 5px;
text-align: right;
}
button {
font-size: 18px;
width: 40px;
height: 40px;
margin: 5px;
background: #eee;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background: #ddd;
}
button.operator {
background: #f90;
color: #fff;
}
button#clear {
background: #ccc;
}
button#equal {
background: #5cb85c;
color: #fff;
}
```
JS 代码:
```js
const display = document.querySelector('.display');
const operators = document.querySelectorAll('.operator');
const numbers = document.querySelectorAll('.number');
const clear = document.querySelector('#clear');
const equal = document.querySelector('#equal');
let firstOperand = '';
let secondOperand = '';
let currentOperator = null;
function clearDisplay() {
firstOperand = '';
secondOperand = '';
currentOperator = null;
display.textContent = '0';
}
function calculate() {
let result = 0;
switch (currentOperator) {
case '+':
result = parseFloat(firstOperand) + parseFloat(secondOperand);
break;
case '-':
result = parseFloat(firstOperand) - parseFloat(secondOperand);
break;
case '*':
result = parseFloat(firstOperand) * parseFloat(secondOperand);
break;
case '/':
result = parseFloat(firstOperand) / parseFloat(secondOperand);
break;
case '%':
result = parseFloat(firstOperand) % parseFloat(secondOperand);
break;
}
display.textContent = result;
firstOperand = result;
secondOperand = '';
currentOperator = null;
}
numbers.forEach(number => {
number.addEventListener('click', () => {
if (currentOperator === null) {
firstOperand += number.value;
display.textContent = firstOperand;
} else {
secondOperand += number.value;
display.textContent = secondOperand;
}
});
});
operators.forEach(operator => {
operator.addEventListener('click', () => {
if (currentOperator !== null) {
calculate();
}
currentOperator = operator.value;
});
});
equal.addEventListener('click', () => {
calculate();
});
clear.addEventListener('click', () => {
clearDisplay();
});
```
这个计算器非常简单,仅供参考。你可以在此基础上进行修改和扩展,例如添加更多的运算符和函数等。
阅读全文