<iframe id="HtmlEdit" style="width: 100%; height: 300px;"></iframe>
<textarea id="hiddenInput" name="content" style="display:none;"></textarea>
<button onclick="syncToTextarea()">同步到表單</button>
<button onclick="syncToIframe()">從表單恢復</button>
<script>
const HtmlEdit = document.getElementById('HtmlEdit');
const hiddenInput = document.getElementById('hiddenInput');
// 1. 初始化編輯模式
function initEditor() {
// 等待 iframe 加載完成
HtmlEdit.onload = () => {
const doc = HtmlEdit.contentDocument || HtmlEdit.contentWindow.document;
doc.designMode = 'On'; // 關鍵:啟用編輯模式
doc.body.style.fontFamily = 'inherit';
doc.body.style.padding = '10px';
};
}
// 2. 獲取 iframe 內容 → 同步到 textarea(表單提交用)
function syncToTextarea() {
const doc = HtmlEdit.contentDocument || HtmlEdit.contentWindow.document;
hiddenInput.value = doc.body.innerHTML;
console.log('提交數據:', hiddenInput.value);
}
// 3. 從 textarea 恢復 → 同步到 iframe
function syncToIframe() {
const doc = HtmlEdit.contentDocument || HtmlEdit.contentWindow.document;
doc.body.innerHTML = hiddenInput.value;
}
// 4. 工具欄命令示例:加粗
function execBold() {
HtmlEdit.contentWindow.focus(); // 關鍵:聚焦到 iframe
const doc = HtmlEdit.contentDocument || HtmlEdit.contentWindow.document;
doc.execCommand('bold', false, null);
}
// 5. 獲取選區並插入 HTML(如插入圖片、鏈接)
function insertHtml(html) {
const win = HtmlEdit.contentWindow;
const doc = win.document;
win.focus();
const sel = doc.getSelection();
if (sel.rangeCount > 0) {
const range = sel.getRangeAt(0);
range.deleteContents();
const fragment = range.createContextualFragment(html);
range.insertNode(fragment);
// 將光標移到插入內容之後
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}
}
// 初始化
initEditor();
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>簡易 iframe 編輯器</title>
<style>
#toolbar button { margin-right: 5px; padding: 5px 10px; }
#HtmlEdit { border: 1px solid #ccc; border-radius: 4px; }
</style>
</head>
<body>
<div id="toolbar">
<button onclick="execCmd('bold')">加粗</button>
<button onclick="execCmd('italic')">斜體</button>
<button onclick="execCmd('underline')">下劃線</button>
<button onclick="insertLink()">插入鏈接</button>
<button onclick="insertImage()">插入圖片</button>
<button onclick="syncToTextarea()">獲取 HTML</button>
</div>
<iframe id="HtmlEdit" style="width: 100%; height: 300px;"></iframe>
<textarea id="output" rows="8" style="width:100%; margin-top:10px; font-family: monospace;" readonly></textarea>
<script>
const frame = document.getElementById('HtmlEdit');
const output = document.getElementById('output');
frame.onload = () => {
const doc = frame.contentDocument;
doc.designMode = 'On';
doc.body.style.cssText = 'font: 14px/1.6 system-ui; padding: 10px;';
};
function execCmd(cmd) {
frame.contentWindow.focus();
frame.contentDocument.execCommand(cmd, false, null);
}
function insertLink() {
const url = prompt('請輸入鏈接地址:');
if (url) execCmd('createLink', url);
}
function insertImage() {
const url = prompt('請輸入圖片地址:');
if (url) execCmd('insertImage', url);
}
function syncToTextarea() {
output.value = frame.contentDocument.body.innerHTML;
}
</script>
</body>
</html>
現代項目建議遷移至 contenteditable + ProseMirror / Slate / TipTap,獲得更好的移動端支持、協同編輯能力與 TypeScript 類型安全。iframe 方案僅適合維護老項目或極簡原型。