
在前端開發中,我們經常需要在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>`);
此時它的行為和雙引号/單引号完全一緻,隻是符号不同。
使用反引号主要是為了利用模闆字符串的特性,或者出于代碼風格的統一。如果不需要這些功能,雙引号或單引号完全夠用。