<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>输入名字转换成对象</title>
</head><body><input type="text" id="nameInput" placeholder="输入名字"><button onclick="convertName()">转换</button><p id="result">输入名字转换成:{name: "安柏",level: 19,constellation: 1,talent: "1/1/1",note: "备注"},</p><script>function convertName() {// 获取用户输入的名字const name = document.getElementById('nameInput').value;// 创建对象const character = {name: name,level: 19,constellation: 1,talent: "1/1/1",note: "备注"};// 生成格式化的 JSON 字符串let jsonString = JSON.stringify(character, null, 2);// 去除属性名的引号jsonString = jsonString.replace(/"(\w+)"\s*:/g, '$1:');// 显示结果document.getElementById('result').innerText = jsonString + ',';}</script>
</body></html>