From 91463b31a3d49c5bf313c530fc0b637ed4965a61 Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Tue, 21 Apr 2026 09:52:01 +0800 Subject: [PATCH] =?UTF-8?q?fix(toolbar):=20=E6=94=AF=E6=8C=81=E5=9C=A8?= =?UTF-8?q?=E9=80=89=E4=B8=AD=E5=85=83=E7=B4=A0=E4=B8=8A=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- js/script.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/js/script.js b/js/script.js index b4d7527..8ca1bcd 100644 --- a/js/script.js +++ b/js/script.js @@ -1680,16 +1680,17 @@ function initTextEditorToolbar() { // 加粗按钮 boldBtn.addEventListener('click', () => { - if (!editingInput) return; + const targetInput = editingInput || selectedInput; + if (!targetInput) return; const selection = window.getSelection(); if (selection.rangeCount > 0 && !selection.isCollapsed) { document.execCommand('bold', false, null); } else { const isBold = boldBtn.classList.contains('active'); if (isBold) { - editingInput.style.fontWeight = 'normal'; + targetInput.style.fontWeight = 'normal'; } else { - editingInput.style.fontWeight = 'bold'; + targetInput.style.fontWeight = 'bold'; } boldBtn.classList.toggle('active'); } @@ -1697,16 +1698,17 @@ // 斜体按钮 italicBtn.addEventListener('click', () => { - if (!editingInput) return; + const targetInput = editingInput || selectedInput; + if (!targetInput) return; const selection = window.getSelection(); if (selection.rangeCount > 0 && !selection.isCollapsed) { document.execCommand('italic', false, null); } else { const isItalic = italicBtn.classList.contains('active'); if (isItalic) { - editingInput.style.fontStyle = 'normal'; + targetInput.style.fontStyle = 'normal'; } else { - editingInput.style.fontStyle = 'italic'; + targetInput.style.fontStyle = 'italic'; } italicBtn.classList.toggle('active'); } @@ -1714,19 +1716,21 @@ // 颜色选择器 colorPicker.addEventListener('input', () => { - if (!editingInput) return; + const targetInput = editingInput || selectedInput; + if (!targetInput) return; const selection = window.getSelection(); if (selection.rangeCount > 0 && !selection.isCollapsed) { document.execCommand('foreColor', false, colorPicker.value); } else { - editingInput.style.color = colorPicker.value; + targetInput.style.color = colorPicker.value; } }); // 字体选择器 fontPicker.addEventListener('change', () => { - if (!editingInput) return; - editingInput.style.fontFamily = fontPicker.value + ', sans-serif'; + const targetInput = editingInput || selectedInput; + if (!targetInput) return; + targetInput.style.fontFamily = fontPicker.value + ', sans-serif'; }); // 字号选择器 - 密码锁式滚轮