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'; }); // 字号选择器 - 密码锁式滚轮