<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>没啥实力的计算器设计</title>
<style>
.waiwei{
border: 2px solid black;
margin: auto;
}
.toubu {
margin: auto;
}
.xianshi{
width: 210px;
}
#func{
background-color:#E15572;
border: 0cap;
color: aliceblue;
border-radius: 3px;
width: 70px;
height: 25px;
}
.kongge{
width: 90px;
}
.kongge1{
width: 25px;
}
.js{
border-radius: 3px;
width: 70px;
}
</style>
</head>
<body>
<table class="waiwei">
<th>
<table class="toubu">
<tr>
<td class="title">
网页版计算器
</td>
</tr>
</table>
<table class="sub">
<tr>
<td colspan="2">
<input type="text" class="xianshi" placeholder="POWER OFF">
</td>
<td>
<input type="button" value="power" id="func" class="js">
</td>
</tr>
</table>
<table class="yunsuan">
<tr></tr>
<tr>
<td class="kongge1"></td>
<td class="f1">
<input type="button" value="clear" id="func" class="js">
</td>
<td class="kongge"></td>
<td class="f2">
<input type="button" value="back" id="func" class="js">
</td>
</tr>
</table>
<table class="anniu">
<tr></tr>
<tr>
<td>
<input type="button" value="1" class="js">
</td>
<td>
<input type="button" value="2" class="js">
</td>
<td>
<input type="button" value="3" class="js">
</td>
<td>
<input type="button" value="4" class="js">
</td>
</tr>
<tr>
<td>
<input type="button" value="5" class="js">
</td>
<td>
<input type="button" value="6" class="js">
</td>
<td>
<input type="button" value="7" class="js">
</td>
<td>
<input type="button" value="8" class="js">
</td>
</tr>
<tr>
<td>
<input type="button" value="9" class="js">
</td>
<td>
<input type="button" value="0" class="js">
</td>
<td>
<input type="button" value="." class="js">
</td>
<td>
<input type="button" value="=" class="js">
</td>
</tr>
<tr>
<td>
<input type="button" value="+" class="js">
</td>
<td>
<input type="button" value="-" class="js">
</td>
<td>
<input type="button" value="*" class="js">
</td>
<td>
<input type="button" value="/" class="js">
</td>
</tr>
</table>
</th>
</table>
<script>
var display = document.querySelector('.xianshi');
var operators = {
'+': function(x, y) { return x + y; },
'-': function(x, y) { return x - y; },
'*': function(x, y) { return x * y; },
'/': function(x, y) { return y ? x / y : 'Error'; }
};
var count=0;
var flag=0;
function countpower(){
count+=1;
if(count%2==0||count==0){
display.value='';
flag=0;
}
else{
display.value='0';
}
}
function updateDisplay(value) {
display.value += value;
}
function clearDisplay() {
display.value = '0';
}
function evaluate() {
try {
var result = eval(display.value);
if (!isNaN(result)) {
display.value = result;
} else {
display.value = 'Error';
}
} catch (e) {
display.value = 'Error';
}
}
function handleButtonClick(event) {
var buttonValue = event.target.value;
if (buttonValue=='power'){
countpower();
}
if(count%2!=0){
if (buttonValue === 'clear') {
clearDisplay();
} else if (buttonValue === 'back') {
display.value = display.value.slice(0, -1);
} else if (buttonValue === '=') {
evaluate();
} else{
if(buttonValue!='power')
{
flag+=1;
if(flag==1){
display.value='';}
updateDisplay(buttonValue);
}
}
}
}
var buttons = document.querySelectorAll('.js');
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', handleButtonClick);
}
</script>
</body>
</html>
power按钮是计算器开关
效果图: