就是这个样的粗爆,手搓一个计算器: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格式化计算器

相关推荐
GIS之路1 天前
ArcGIS Pro 中的 Python 入门
前端
树獭非懒1 天前
告别繁琐多端开发:DivKit 带你玩转 Server-Driven UI!
android·前端·人工智能
兆子龙1 天前
当「多应用共享组件」成了刚需:我们从需求到模块联邦的落地小史
前端·架构
Qinana1 天前
从代码到智能体:MCP 协议如何重塑 AI Agent 的边界
前端·javascript·mcp
Wect1 天前
LeetCode 130. 被围绕的区域:两种解法详解(BFS/DFS)
前端·算法·typescript
不会敲代码11 天前
从入门到进阶:手写React自定义Hooks,让你的组件更简洁
前端·react.js
用户5433081441941 天前
拆完 Upwork 前端我沉默了:你天天卷的那些技术,人家根本没用
前端
洋洋技术笔记1 天前
Vue实例与数据绑定
前端·vue.js
Marshall1511 天前
zzy-scroll-timer:一个跨框架的滚动定时器插件
前端·javascript
明月_清风1 天前
打字机效果优化:用 requestAnimationFrame 缓冲高频文字更新
前端·javascript