就是这个样的粗爆,手搓一个计算器:JSON格式化计算器

作为程序员,没有合适的工具,就得手搓一个,PC端,移动端均可适用。废话不多说,直接上代码。

HTML:

html 复制代码
<div class="calculator"><label for="jsonInput">输入 JSON 字符串:</label> <textarea id="jsonInput" rows="10" placeholder="请输入 JSON 字符串"></textarea><button onclick="formatJSON()">格式化</button><div class="result"><div>格式化结果:</div><pre id="jsonOutput" style="color: black; text-align: left;">结果将显示在此处</pre></div></div>

JS:

javascript 复制代码
function formatJSON() {
    const jsonInput = document.getElementById('jsonInput').value.trim();

    if (jsonInput === '') {
        alert('请输入 JSON 字符串');
        return;
    }

    try {
        const jsonObject = JSON.parse(jsonInput);
        const formattedJSON = JSON.stringify(jsonObject, null, 4);
        document.getElementById('jsonOutput').textContent = formattedJSON;
    } catch (e) {
        alert('无效的 JSON 字符串,请检查输入内容');
        document.getElementById('jsonOutput').textContent = '无效的 JSON 字符串';
    }
}

CSS:

css 复制代码
.calculator {
    width: 100%;
    background-color: #333;
    color: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

textarea {
        width: 100%;
    margin-bottom: 10px;
    }

label {
    display: block;
    margin-bottom: 10px;
    font-size: 16px;
}

input, select {
    width: 100%!important;
    padding: 10px!important;
    margin-bottom: 20px;
       color: #000000; 
    border-radius: 5px;
    border: 1px solid #555;
    font-size: 16px!important;
    background-color: #ffffff!important;
}

button {
    width: 100%;
    padding: 10px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
}

button:hover {
    background-color: orange;
}

.result {
    margin-top: 20px;
    text-align: center;
}

option {
        background-color: #ffffff;
}


p {
    font-size: 18px;
        margin-top: 5px!important;
}

线上运行,可以直接打开:JSON格式化计算器

相关推荐
修炼的dance22 分钟前
白底图存JPG,sharp开mozjpeg背景杂点少近三成
前端
anOnion31 分钟前
构建无障碍组件之Grid Pattern
前端·html·交互设计
乔旭伟1 小时前
Vben3 中后台表格封装实战:useVbenVxeGrid 的全局收敛与七个坑
前端·anti-design-vue·1024程序员节
鶴哥只手遮天1 小时前
jQuery 4.0 源码架构与工程实践-第 10 章 尺寸、几何与显隐
前端·javascript·jquery
Mia_墨黎1 小时前
2026 Windows 11 终端美化完全指南
前端·后端
用户1880687493521 小时前
戴着 Rokid 接成语:用 AIUI 做一局有提示、有复盘的“接着说”
前端
Sakura_j_h_2 小时前
Three.js 单文件离线机械臂工作台:爆炸图、透明外壳与剖面
前端·机械臂·webgl·three.js·1024程序员节·esbuild
计算机魔术师3 小时前
管谷歌的不是Pichai一个人,是一整层AI智能体
前端
米米商聊3 小时前
图标能点,按钮就可用了吗?键盘、名称与收藏状态
前端·javascript
Younglina4 小时前
三个晚上,我和 WorkBuddy 做了个生图网站
前端·后端·ai编程