Files

333 lines
10 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<link rel="icon" href="../../favicon.svg">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML 实体编码/解码</title>
<link rel="canonical" href="https://www.htmls.dev/tools/html-entity/app.html">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
text-align: center;
color: white;
margin-bottom: 30px;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
.card {
background: rgba(255, 255, 255, 0.95);
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.editor-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.editor-panel {
background: #f8f9fa;
border-radius: 12px;
padding: 15px;
}
.editor-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.editor-title {
font-weight: 600;
color: #333;
font-size: 1.1rem;
}
.editor-textarea {
width: 100%;
min-height: 350px;
padding: 15px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-family: 'Courier New', monospace;
font-size: 14px;
resize: vertical;
transition: border-color 0.3s;
}
.editor-textarea:focus {
outline: none;
border-color: #f5576c;
}
.action-bar {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 20px;
}
.btn {
padding: 12px 24px;
border: none;
border-radius: 10px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
color: white;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 10px 25px rgba(245, 87, 108, 0.4);
}
.btn-secondary {
background: #6c757d;
}
@media (max-width: 900px) {
.editor-container {
grid-template-columns: 1fr;
}
}
.tool-doc-link,
.tool-home-link {
position: fixed;
top: 16px;
padding: 6px 12px;
border-radius: 999px;
font-size: 12px;
font-weight: 600;
color: #0f172a;
background: rgba(255, 255, 255, 0.9);
border: 1px solid rgba(15, 23, 42, 0.12);
text-decoration: none;
letter-spacing: 0.02em;
z-index: 9999;
box-shadow: 0 6px 16px rgba(15, 23, 42, 0.08);
transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
}
.tool-doc-link {
right: 16px;
}
.tool-home-link {
right: 74px;
}
.tool-doc-link:hover,
.tool-home-link:hover {
background: rgba(255, 255, 255, 1);
border-color: rgba(15, 23, 42, 0.2);
transform: translateY(-1px);
}
</style>
</head>
<body>
<a class="tool-home-link" href="../../index.html">首页</a>
<a class="tool-doc-link" href="index.html">说明</a>
<div class="container">
<div class="header">
<h1>HTML 实体编码/解码</h1>
<p>将 HTML 特殊字符转换为实体编码或反向转换</p>
</div>
<div class="card">
<div class="action-bar">
<button class="btn" onclick="encode()">编码 →</button>
<button class="btn" onclick="decode()">← 解码</button>
<button class="btn btn-secondary" onclick="loadSample()">示例</button>
<button class="btn btn-secondary" onclick="clearAll()">清空</button>
</div>
<div class="editor-container">
<div class="editor-panel">
<div class="editor-header">
<span class="editor-title">输入</span>
<button class="btn btn-secondary" style="padding: 6px 12px; font-size: 12px;" onclick="pasteToInput()">粘贴</button>
</div>
<textarea class="editor-textarea" id="input" placeholder="输入要编码/解码的文本..."></textarea>
</div>
<div class="editor-panel">
<div class="editor-header">
<span class="editor-title">输出</span>
<button class="btn btn-secondary" style="padding: 6px 12px; font-size: 12px;" onclick="copyOutput()">复制</button>
</div>
<textarea class="editor-textarea" id="output" placeholder="输出结果..." readonly></textarea>
</div>
</div>
</div>
</div>
<script>
const htmlEntities = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'©': '&copy;',
'®': '&reg;',
'€': '&euro;',
'£': '&pound;',
'¥': '&yen;',
'¢': '&cent;',
'§': '&sect;',
'¶': '&para;',
'•': '&bull;',
'…': '&hellip;',
'°': '&deg;',
'±': '&plusmn;',
'×': '&times;',
'÷': '&divide;',
'½': '&frac12;',
'¼': '&frac14;',
'¾': '&frac34;',
'√': '&radic;',
'∞': '&infin;',
'≈': '&asymp;',
'≠': '&ne;',
'≤': '&le;',
'≥': '&ge;',
'←': '&larr;',
'→': '&rarr;',
'↑': '&uarr;',
'↓': '&darr;',
'↔': '&harr;',
'α': '&alpha;',
'β': '&beta;',
'γ': '&gamma;',
'δ': '&delta;',
'ε': '&epsilon;',
'π': '&pi;',
'ω': '&omega;',
'Σ': '&Sigma;',
};
function encode() {
const input = document.getElementById('input').value;
let output = input;
// Encode special characters
output = output.replace(/[&<>"']/g, function(char) {
return htmlEntities[char] || char;
});
// Also encode other common entities
output = output.replace(/[©®€£¥¢§¶•…°±×÷½¼¾√∞≈≠≤≥←→↑↓↔αβγδεπωΣ]/g, function(char) {
return htmlEntities[char] || char;
});
document.getElementById('output').value = output;
}
function decode() {
const input = document.getElementById('input').value;
const textarea = document.createElement('textarea');
textarea.innerHTML = input;
document.getElementById('output').value = textarea.value;
}
async function pasteToInput() {
try {
const text = await navigator.clipboard.readText();
document.getElementById('input').value = text;
} catch (err) {
showToast('无法访问剪贴板');
}
}
function copyOutput() {
const output = document.getElementById('output').value;
if (!output) {
showToast('没有可复制的内容');
return;
}
navigator.clipboard.writeText(output).then(() => {
showToast('已复制到剪贴板');
});
}
function clearAll() {
document.getElementById('input').value = '';
document.getElementById('output').value = '';
}
function loadSample() {
document.getElementById('input').value = `<div class="container">
<h1>标题 & 标题</h1>
<p>这是一个 "段落" & 包含特殊字符。</p>
<code>&lt;code&gt;代码块&lt;/code&gt;</code>
<p>版权所有 © 2024 | 价格: €99 | 温度: 25°C</p>
</div>`;
encode();
}
function showToast(message) {
const toast = document.createElement('div');
toast.style.cssText = `
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
background: #333;
color: white;
padding: 12px 24px;
border-radius: 8px;
z-index: 1000;
animation: fadeInOut 2s ease;
`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => toast.remove(), 2000);
}
// Add animation
const style = document.createElement('style');
style.textContent = `
@keyframes fadeInOut {
0% { opacity: 0; transform: translateX(-50%) translateY(20px); }
15% { opacity: 1; transform: translateX(-50%) translateY(0); }
85% { opacity: 1; transform: translateX(-50%) translateY(0); }
100% { opacity: 0; transform: translateX(-50%) translateY(-20px); }
}
`;
document.head.appendChild(style);
</script>
<script src="../../assets/clicks.js" defer></script>
</body>
</html>