HTML CSS JS学习笔记之实现网页计算器2,0基础前端开发
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
<style>
body { font-family: Arial, sans-serif; }
form { text-align: center; }
input[type="text"] { width: 240px; }
input[type="button"] { width: 40px; }
</style>
</head>
<body>
<form onsubmit="return false;">
<input type="text" id="display" disabled>
<br>
<input type="button" value="1" onclick="press('1')">
<input type="button" value="2" onclick="press('2')">
<input type="button" value="3" onclick="press('3')">
<input type="button" value="+" onclick="press('+')">
<br>
<input type="button" value="4" onclick="press('4')">
<input type="button" value="5" onclick="press('5')">
<input type="button" value="6" onclick="press('6')">
<input type="button" value="-" onclick="press('-')">
<br>
<input type="button" value="7" onclick="press('7')">
<input type="button" value="8" onclick="press('8')">
<input type="button" value="9" onclick="press('9')">
<input type="button" value="*" onclick="press('*')">
<br>
<input type="button" value="0" onclick="press('0')">
<input type="button" value="Clear" onclick="clearDisplay()">
<input type="button" value="=" onclick="calculateResult()">
<input type="button" value="/" onclick="press('/')">
</form>
<script>
var display = document.getElementById("display");
var operator = "";
var firstNumber = 0;
var waitingForOperand = true;
function press(buttonText) {
if (waitingForOperand) {
display.value = buttonText;
waitingForOperand = false;
} else {
display.value += buttonText;
}
}
function clearDisplay() {
display.value = "";
waitingForOperand = true;
}
function calculateResult() {
var input = display.value;
评论已关闭