上一篇
html如何计算器
- 行业动态
- 2024-03-27
- 1
要创建一个HTML计算器,你需要使用HTML、CSS和JavaScript,以下是详细步骤:
1、创建HTML结构
我们需要创建一个HTML文件,然后在其中添加一个表单,用于显示计算器的输入框和按钮。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <meta name="viewport" content="width=devicewidth, initialscale=1.0"> <title>简易计算器</title> </head> <body> <form id="calculator"> <input type="text" id="display" readonly> <br> <button type="button" onclick="clearDisplay()">C</button> <button type="button" onclick="appendNumber(7)">7</button> <button type="button" onclick="appendNumber(8)">8</button> <button type="button" onclick="appendNumber(9)">9</button> <button type="button" onclick="appendOperator('+')">+</button> <br> <button type="button" onclick="appendNumber(4)">4</button> <button type="button" onclick="appendNumber(5)">5</button> <button type="button" onclick="appendNumber(6)">6</button> <button type="button" onclick="appendOperator('')"></button> <br> <button type="button" onclick="appendNumber(1)">1</button> <button type="button" onclick="appendNumber(2)">2</button> <button type="button" onclick="appendNumber(3)">3</button> <button type="button" onclick="appendOperator('*')">*</button> <br> <button type="button" onclick="appendNumber(0)">0</button> <button type="button" onclick="appendOperator('.')">.</button> <button type="button" onclick="calculateResult()">=</button> <button type="button" onclick="appendOperator('/')">/</button> </form> <script src="calculator.js"></script> </body> </html>
2、编写CSS样式
接下来,我们需要为计算器添加一些基本的样式,在<head>
标签内添加以下代码:
<style> body { fontfamily: Arial, sansserif; display: flex; justifycontent: center; alignitems: center; height: 100vh; margin: 0; } form { border: 1px solid #ccc; padding: 20px; borderradius: 5px; } input { width: 100%; marginbottom: 10px; textalign: right; } button { width: 22%; margin: 1%; fontsize: 18px; } </style>
3、编写JavaScript代码
我们需要编写JavaScript代码来处理计算器的功能,创建一个名为calculator.js
的文件,并在其中添加以下代码:
const display = document.getElementById('display'); let currentInput = ''; let operator = null; let firstOperand = null; let shouldReset = false; function appendNumber(number) { if (shouldReset) { resetCalculator(); } currentInput += number; display.value = currentInput; } function appendOperator(op) { if (operator !== null) { calculateResult(); } firstOperand = parseFloat(currentInput); operator = op; currentInput = ''; } function clearDisplay() { currentInput = ''; operator = null; firstOperand = null; shouldReset = false; display.value = ''; } function calculateResult() { const secondOperand = parseFloat(currentInput); let result = 0; switch (operator) { case '+': result = firstOperand + secondOperand; break; case '': result = firstOperand secondOperand; break; case '*': result = firstOperand * secondOperand; break; case '/': result = firstOperand / secondOperand; break; default: return; } display.value = result; currentInput = ''; operator = null; firstOperand = null; shouldReset = true; }
现在,你可以在浏览器中打开HTML文件,查看并使用你的简易计算器了。
本站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本站,有问题联系侵删!
本文链接:http://www.xixizhuji.com/fuzhu/282823.html