计算器原生js

目录

1.HTML

2.CSS

2.JS

4.资源

5.运行截图

6.下载连接

7.注意事项


1.HTML

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算器</title>
    <link rel="stylesheet" href="../css/calculator.css">
    <style>
        .calculator{
            width: 250px;
            height: 430px;
            margin: 100px auto;
            border: 1px #ccc solid;
        }
        h3{
            width: 100%;
            height: 50px;
            border-bottom: 1px #ccc solid;
        }
        h4{
            width: 100%;
            height: 30px;
            border-bottom: 1px #ccc solid;
        }
        .list{
            width: 100%;
            height: 350px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            align-content: space-around;
        }
        .list li{
            width: 50px;
            height: 50px;
            line-height: 50px;
            text-align: center;
            border: 1px #ccc solid;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h3></h3>
        <h4></h4>
        <ul class="list">
            <!-- <li>AC</li>
            <li>%</li>
            <li>←</li>
            <li>÷</li>
            <li>7</li>
            <li>8</li>
            <li>9</li>
            <li>×</li>
            <li>4</li>
            <li>5</li>
            <li>6</li>
            <li>-</li>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>+</li>
            <li>00</li>
            <li>0</li>
            <li>.</li>
            <li>=</li>  -->
        </ul>
    </div>
</body>
</html>
<script src="../js/calculator.js"></script>
<script>
    let list = document.querySelector('.list');
    let h3 = document.querySelector('h3');
    let h4 = document.querySelector('h4');
    let listData = ['AC','%','←','÷',7,8,9,'×',4,5,6,'-',1,2,3,'+','00',0,'.',"="];
    listData.forEach(item => {
        let li = `<li>${item}</li>`;
        list.innerHTML+=li;
    });
    let li = list.querySelectorAll('li');
    list.addEventListener('click',(e)=>{
        let target = e.target;
        // 检查点击的目标是否是 li 元素
        if (target.tagName.toLowerCase() === 'li') {
            h3.innerHTML+=target.innerHTML;
            console.log(h3.innerHTML.slice(-2),'qqq');
            if(h3.innerHTML.slice(-2).match(/^[\+\-\×\÷\%]+$/)){
                console.log(h3.innerHTML);
                h3.innerHTML=h3.innerHTML.substring(0, h3.innerHTML.length - 2) + target.innerHTML;
            };
            if(target.innerHTML=='←'){
                let txt = h3.innerHTML;
                console.log(txt.length);
                h3.innerHTML=txt.substring(0, txt.length - 2);
                console.log(target,h3.innerHTML);
            }
            if(target.innerHTML=='AC'){
                h3.innerHTML='';
                h4.innerHTML='';
            }
            if(target.innerHTML=='='){
                let txt = h3.innerHTML.replace(/\u00F7/g, "/").replace(/\u00D7/g, "*");
                h3.innerHTML=h3.innerHTML.substring(0, txt.length - 1);
                let data = {
                    txt:txt.substring(0, txt.length - 1)
                };
                fetch("http://localhost:8080/api/calculator", {
                    method: "POST",
                    headers: {
                    'Content-Type': 'application/json', // 设置请求头的 Content-Type
                    },
                    body: JSON.stringify(data),
                }).then(response => response.json()).then(res => {
                    h4.innerHTML=res.answer;
                    console.log("res:", res);
                }).catch(err => {
                    console.log("err:", err);
                });
            }

            // 阻止事件冒泡
            e.stopPropagation(); 
        }
    });

</script>

2.CSS

css 复制代码
*{margin: 0;padding: 0;}
ul,li,ol{list-style-type: none;}
button{cursor: pointer;border: none;background: rgba(255, 255, 255, 1);}

2.JS

javascript 复制代码
let list = document.querySelector('.list');
let h3 = document.querySelector('h3');
let h4 = document.querySelector('h4');
let listData = ['AC','%','←','÷',7,8,9,'×',4,5,6,'-',1,2,3,'+','00',0,'.',"="];
listData.forEach(item => {
    let li = `<li>${item}</li>`;
    list.innerHTML+=li;
});
let li = list.querySelectorAll('li');
list.addEventListener('click',(e)=>{
    let target = e.target;
    // 检查点击的目标是否是 li 元素
    if (target.tagName.toLowerCase() === 'li') {
        h3.innerHTML+=target.innerHTML;
        console.log(h3.innerHTML.slice(-2),'qqq');
        if(h3.innerHTML.slice(-2).match(/^[\+\-\×\÷\%]+$/)){
            console.log(h3.innerHTML);
            h3.innerHTML=h3.innerHTML.substring(0, h3.innerHTML.length - 2) + target.innerHTML;
        };
        if(target.innerHTML=='←'){
            let txt = h3.innerHTML;
            console.log(txt.length);
            h3.innerHTML=txt.substring(0, txt.length - 2);
            console.log(target,h3.innerHTML);
        }
        if(target.innerHTML=='AC'){
            h3.innerHTML='';
            h4.innerHTML='';
        }
        if(target.innerHTML=='='){
            let txt = h3.innerHTML.replace(/\u00F7/g, "/").replace(/\u00D7/g, "*");
            h3.innerHTML=h3.innerHTML.substring(0, txt.length - 1);
            let data = {
                txt:txt.substring(0, txt.length - 1)
            };
            fetch("http://localhost:8080/api/calculator", {
                method: "POST",
                headers: {
                'Content-Type': 'application/json', // 设置请求头的 Content-Type
                },
                body: JSON.stringify(data),
            }).then(response => response.json()).then(res => {
                h4.innerHTML=res.answer;
                console.log("res:", res);
            }).catch(err => {
                console.log("err:", err);
            });
        }

        // 阻止事件冒泡
        e.stopPropagation(); 
    }
});

4.资源

5.运行截图

6.下载连接

在此处下载压缩包

【免费】原生js配合Node.js的计算器资源-CSDN文库

7.注意事项

此压缩包 包含前后端简单交互,后端需要用到Node.js,运行口令 nodemon app.js

或者在serve文件夹直接运行run.bat文件,运行成功后打开html即可。注意:run.bat运行成功后不要退出cmd。

相关推荐
AI大模型-小华5 小时前
Codex CLI第一次怎么用?从安装到读取本地项目完整教程
git·node.js·ai编程·开发工具·代码分析·codex·codex cli
BillKu5 小时前
vue3 字符串排序 localeCompare,确保排序为 “B01“, “B10“, “B2“
前端·javascript·vue.js
晴天166 小时前
打造自己的 npm 包实战指南
前端·npm·node.js
然我6 小时前
从 Service 到生命周期:Agent Runtime 的插件内核
前端·javascript·agent
用户6919026813396 小时前
Agent 记忆处理机制详解:从变量数据类型到内存模型
javascript·架构
ShineWinsu6 小时前
对于 Vue 3:开发前需要掌握的 JavaScript 核心基础:从变量、数组方法到 Promise、Async/Await 与 ES Module 的解析
javascript·vue.js·elasticsearch
西西小飞龙6 小时前
npm vs pnpm
前端·npm·node.js
晴天167 小时前
Tsup:TypeScript库打包的“零配置极速方案”
前端·javascript·typescript
FungLeo7 小时前
成为全栈·Node 后端篇·权限模型:从认证到 RBAC
node.js·rbac·用户认证·权限模型·成为全栈
雪芽蓝域zzs7 小时前
第十七节:面包屑导航组件(适配多级嵌套路由)
前端·javascript·vue.js