
在前端开发中,我们经常需要在HTML代码和JavaScript动态生成代码之间来回切换。例如,将一段HTML片段转换为通过document.write
或document.writeln
输出的JS代码,或者将已有的JS代码还原为原始HTML结构。手动处理这些转换不仅繁琐,还容易因转义字符、换行符等问题导致错误。为此,我们开发了一款HTML与JS代码双向转换器,支持实时自动转换、多种格式兼容,让代码转换变得高效且零错误。
document.writeln
语句,使用双引号包裹内容,确保特殊字符(如双引号)正确转义。 document.write(
...)
) document.writeln("...")
)'
、"
、```)的代码,无需手动转义。 document.writeln
代码还原为易读的HTML结构,方便重构和调试。 document.write
/writeln
的代码(支持模板字符串或双引号字符串)。输入完成后,右侧对应输出框会实时显示转换结果:
document.writeln
,双引号包裹)。 <div class="container">
<h1>Hello, World!</h1>
<p>这是一段HTML代码。</p>
</div>
document.writeln("<div class=\"container\">");
document.writeln(" <h1>Hello, World!</h1>");
document.writeln(" <p>这是一段HTML代码。</p>");
document.writeln("</div>");
使用四个<textarea>
输入框,通过oninput
事件实时触发转换函数:
<div class="input-container">
<label>输入HTML代码</label>
<textarea id="htmlInput" oninput="htmlToJs()"></textarea>
</div>
<div class="input-container">
<label>转换后的JS代码</label>
<textarea id="jsOutput" readonly></textarea>
</div>
<!-- 另外两组输入框类似结构 -->
简洁的响应式设计,支持自适应宽度,输入框聚焦时显示蓝色边框,提升交互体验:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
padding: 20px;
}
textarea {
width: 100%;
height: 150px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical;
}
textarea:focus {
border-color: #007BFF;
outline: none;
}
HTML转JS(htmlToJs
):
按行分割HTML代码,转义双引号并生成document.writeln
语句:
function htmlToJs() {
const html = document.getElementById('htmlInput').value;
const lines = html.split('\n');
let js = '';
for (let line of lines) {
// 转义双引号,避免JS字符串冲突
const escapedLine = line.replace(/"/g, '\\"');
js += `document.writeln("${escapedLine}");\n`;
}
document.getElementById('jsOutput').value = js;
}
JS转HTML(jsToHtml
):
使用正则表达式分别匹配模板字符串和双引号字符串,提取内容并还原:
function jsToHtml() {
const js = document.getElementById('jsInput').value;
let html = '';
// 匹配模板字符串(反引号)
const templateRegex = /document\.(write|writeln)\(\s*`([\s\S]*?)`\s*\);/g;
// 匹配双引号字符串
const stringRegex = /document\.(write|writeln)\(\s*"([^"]*)"\s*\);/g;
// 提取模板字符串内容
let match;
while ((match = templateRegex.exec(js))!== null) {
html += match[2].replace(/\\`/g, '`'); // 还原反引号
}
// 提取双引号字符串内容
while ((match = stringRegex.exec(js))!== null) {
html += match[2] + '\n'; // 无需转义,直接拼接
}
document.getElementById('htmlOutput').value = html;
}
"
→ \"
),无需手动处理。 document.write
/writeln
语句格式正确(以分号结尾,引号匹配)。document.write("...");
结构。 这款HTML与JS代码双向转换器工具通过简洁的设计和强大的兼容性,解决了前端开发中常见的代码格式转换问题。无论是快速生成动态JS代码,还是还原复杂HTML结构,它都能高效完成,减少手动操作带来的错误。建议前端开发者、学习者收藏使用,或嵌入项目中作为辅助工具,提升开发效率。
在 JavaScript 中,`document.write()` 的参数可以是任何字符串。通常情况下,字符串可以用双引号 `"` 或单引号 `'` 包裹,例如:
document.write("<p>Hello World</p>");
推荐使用`
(即 模板字符串/Template Literals),原因:
反引号 `
是 ES6 引入的模板字符串语法,它提供了更强大的功能:
\n
或拼接符号。${variable}
嵌入变量或表达式。const name = "Alice";
document.write(`
<div>
<h1>Hello ${name}</h1>
<p>Current time: ${new Date().toLocaleTimeString()}</p>
</div>
`);
这里反引号允许直接编写多行 HTML,并动态插入变量 name
和表达式结果。
即使没有使用模板字符串的高级功能,开发者也可能出于以下原因选择反引号:
"
或单引号 '
,用反引号可以避免频繁转义。反引号本身也可以作为普通字符串的包裹符号(不涉及变量插值或多行):
document.write(`<p>This is a normal string</p>`);
此时它的行为和双引号/单引号完全一致,只是符号不同。
使用反引号主要是为了利用模板字符串的特性,或者出于代码风格的统一。如果不需要这些功能,双引号或单引号完全够用。