如何制作一个简单的HTML计算器
(图片来源网络,侵删)
在这篇文章中,我们将学习如何使用HTML和JavaScript制作一个简单的计算器,以下是我们将要完成的主要步骤:
1、创建HTML结构
2、添加CSS样式
3、编写JavaScript代码
4、测试和调试
1. 创建HTML结构
我们需要创建一个HTML文件,并在其中添加以下结构:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF8">
<meta name="viewport" content="width=devicewidth, initialscale=1.0">
<title>简单计算器</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="calculator">
<input type="text" class="display" readonly>
<div class="buttons">
<!在这里添加按钮 >
</div>
</div>
<script src="scripts.js"></script>
</body>
</html>
接下来,我们需要在<div class="buttons">
中添加一些按钮,如数字、加减乘除等,我们可以使用<button>
标签来实现这一点。
<button class="number">1</button>
<button class="number">2</button>
<button class="number">3</button>
<!... >
<button class="operator">+</button>
<button class="operator"></button>
<button class="operator">*</button>
<button class="operator">/</button>
2. 添加CSS样式
为了美化我们的计算器,我们可以为其添加一些CSS样式,创建一个名为styles.css
的文件,并添加以下内容:
body {
display: flex;
justifycontent: center;
alignitems: center;
height: 100vh;
margin: 0;
backgroundcolor: #f0f0f0;
}
.calculator {
border: 1px solid #ccc;
borderradius: 5px;
backgroundcolor: #fff;
padding: 20px;
width: 300px;
}
.display {
width: 100%;
marginbottom: 20px;
textalign: right;
fontsize: 24px;
}
.buttons {
display: grid;
gridtemplatecolumns: repeat(4, 1fr);
gap: 5px;
}
将此CSS文件链接到我们的HTML文件中,现在,我们的计算器应该看起来更漂亮了。
3. 编写JavaScript代码
接下来,我们需要编写JavaScript代码来处理按钮点击事件,创建一个名为scripts.js
的文件,并添加以下内容:
const display = document.querySelector('.display');
const buttons = Array.from(document.querySelectorAll('button'));
let firstValue = '';
let secondValue = '';
let operator = null;
let waitingForSecondValue = false;
const clear = () => {
display.value = '';
firstValue = '';
secondValue = '';
operator = null;
};
const calculate = () => {
if (firstValue === '' || secondValue === '') return; // 如果输入不完整,则返回空字符串或NaN值,如果运算符为null,则直接返回firstValue,如果运算符不为null,则根据运算符进行相应的计算,将结果显示在显示器上,如果没有输入任何内容(即firstValue和secondValue都为空),则清除显示器上的任何内容,否则,执行相应的操作,如果正在等待第二个值(即waitingForSecondValue为true),则将第一个值设置为当前显示的值,将waitingForSecondValue设置为false,以便可以继续接收第二个值,如果已经输入了两个值(即waitingForSecondValue为false),则将第一个值设置为当前显示的值,并根据运算符执行相应的操作,将waitingForSecondValue设置为true,以便可以等待第二个值,将结果存储在firstValue变量中,以便可以将其显示在显示器上,如果用户点击了“+”或“”,则将运算符设置为对应的值,如果用户点击了“*”或“/”,则将运算符设置为对应的值,如果用户点击了“=”,则执行相应的操作,如果用户点击了“C”,则清除显示器上的任何内容,如果用户点击了数字或小数点,则将其添加到显示器上的数字中,如果用户点击了运算符或括号,则将其添加到显示器上的数字中,如果用户点击了“C”,则清除显示器上的任何内容,如果用户点击了数字或小数点,则将其添加到显示器上的数字中,如果用户点击了运算符或括号,则将其添加到显示器上的数字中,如果用户点击了“C”,则清除显示器上的任何内容,if (operator !== null) { // 如果已经有运算符存在,则执行相应的操作,否则,将第一个值存储在firstValue变量中,以便可以将其显示在显示器上,if (operator === '+' || operator === '') { // 如果运算符是加法或减法,则执行相应的操作,result = parseFloat(firstValue) + parseFloat(secondValue); } else if (operator === '*' || operator === '/') { // 如果运算符是乘法或除法,则执行相应的操作,result = parseFloat(firstValue) * parseFloat(secondValue); } else if (operator === '%') { // 如果运算符是取模,则执行相应的操作,result = parseFloat(firstValue) % parseFloat(secondValue); } } display.value = result; // 将结果显示在显示器上,clear(); // 清除显示器上的任何内容,waitingForSecondValue = true; // 设置等待第二个值的状态为true。} else if (event.target.classList.contains('operator')) { // 如果用户点击了一个运算符或括号,则将其添加到显示器上的数字中,并将运算符存储在operator变量中,if (event.target.textContent === '=') { // 如果用户点击了“=”,则执行相应的操作,if (waitingForSecondValue) { // 如果正在等待第二个值,则执行相应的操作,result = performOperation(firstValue, secondValue, operator); // 执行相应的操作。} else { // 如果已经输入了两个值,则执行相应的操作,result = performOperation(firstValue, secondValue, operator); // 执行相应的操作。} display.value = result; // 将结果显示在显示器上,clear(); // 清除显示器上的任何内容,waitingForSecondValue = true; // 设置等待第二个值的状态为true。} else { // 如果用户点击了一个运算符或括号,则将其添加到显示器上的数字中,并将运算符存储在operator变量中,operator = event.target.textContent; // 将运算符存储在operator变量中,firstValue += event.target.textContent; // 将运算符添加到显示器上的数字中。} } else if (event.target.classList.contains('decimal')) { // 如果用户点击了一个小数点,则将其添加到显示器上的数字中,if (!waitingForSecondValue) { // 如果还没有输入第二个值,则将小数点添加到显示器上的数字中,firstValue += '.'; } } else if (event.target.classList.contains('clear')) { // 如果用户点击了“C”,则清除显示器上的任何内容,clear(); } else if (event.target.classList.contains('backspace')) { // 如果用户点击了退格键,则删除显示器上的最后一个字符(如果有的话),if (firstValue !== '') { firstValue = firstValue.slice(0, 1); } } else if (event.target.classList.contains('number')) { // 如果用户点击了一个数字或小数点键,则将其添加到显示器上的数字中,if (!waitingForSecondValue) { // 如果还没有输入第二个值,则将数字添加到显示器上的数字中,firstValue += event.target.textContent; } } else if (event.target.classList.contains('operator')) { // 如果用户点击了一个运算符或括号键,则将其添加到显示器上的数字中,并将运算符存储在operator变量中,operator = event.target.textContent; // 将运算符存储在operator变量中,firstValue += event.target.textContent; // 将运算符添加到显示器上的数字中。} else if (event.target.classList.contains('equal')) { // 如果用户点击了“=”,则执行相应的操作,if (waitingForSecondValue) { // 如果正在等待第二个值,则执行相应的操作,result = performOperation(firstValue, secondValue, operator); // 执行相应的操作。} else { // 如果已经输入了两个值,则执行相应的操作,result = performOperation(firstValue, secondValue, operator); // 执行相应的操作。} display
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)