DIV CSS 佈局教程網

document.execCommand() 指令清單與應用提醒
編輯:JavaScript綜合知識     

document.execCommand() 是早期實作所見即所得編輯器與網頁操作的核心 API。以下整理它的可用指令,並附上幾個實際應用。開宗明義:它在 Firefox 下的支援度不佳,且現已被標記為不建議使用。

版面與定位類

  • 2D-Position:允許透過拖曳移動絕對定位的物件。
  • AbsolutePosition:將元素的 position 設為 absolute。
  • InlineDirLTR / InlineDirRTL / BlockDirLTR / BlockDirRTL:目前尚未支援。

文字樣式類

  • Bold:切換粗體。
  • Italic:切換斜體。
  • Underline:切換下劃線。
  • StrikeThrough / Subscript / Superscript:目前尚未支援。
  • FontName:設定或取得字型。
  • FontSize:設定或取得字級。
  • BackColor:設定或取得選取區的背景色。
  • ForeColor:設定或取得選取區的前景(文字)色。
  • RemoveFormat:移除選取區的格式化標籤。

段落與縮排類

  • FormatBlock:設定目前的區塊格式化標籤。
  • Indent:增加縮排。
  • Outdent:減少縮排。
  • JustifyLeft / JustifyCenter / JustifyRight:對齊。
  • JustifyFull / JustifyNone:目前尚未支援。
  • InsertOrderedList:切換為編號清單。
  • InsertUnorderedList:切換為項目符號清單。
  • InsertParagraph:插入換行。
  • InsertHorizontalRule:插入水平線。

剪貼簿與歷史類

  • Cut:複製選取區到剪貼簿並刪除。
  • Copy:複製選取區到剪貼簿。
  • Paste:以剪貼簿內容覆蓋選取區。
  • Delete:刪除選取區。
  • Undo:復原。
  • Redo:重做。
  • SelectAll:選取整個文件。
  • Unselect:清除選取狀態。

插入元素類

這類指令的共同語意是「用某個控制項覆蓋目前的選取區」:

  • InsertImage:圖片。
  • CreateLink:超連結。
  • Unlink:移除全部超連結。
  • CreateBookmark:建立書籤錨點。
  • UnBookmark:移除全部書籤。
  • InsertInputText / InsertInputPassword / InsertInputCheckbox / InsertInputRadio / InsertInputSubmit / InsertInputReset / InsertInputButton / InsertInputHidden / InsertInputImage / InsertInputFileUpload:各類表單控制項。
  • InsertTextArea:多行文字輸入。
  • InsertSelectDropdown / InsertSelectListbox:下拉選單與清單框。
  • InsertButton:按鈕。
  • InsertFieldset:方框。
  • InsertIFrame:內嵌框架。
  • InsertMarquee:跑馬燈。

編輯器行為類

  • LiveResize:讓 MSHTML 編輯器在縮放或移動過程中持續更新外觀,而非等到動作結束。
  • MultipleSelection:允許按住 Shift 或 Ctrl 一次選取多個元素。
  • OverWrite:切換插入與覆寫模式。
  • Refresh:重新整理文件。
  • Print:開啟列印對話框。
  • SaveAs:將頁面儲存為檔案。
  • Open:開啟。
  • Stop:停止。
  • BrowseMode / EditMode:目前尚未支援。

應用範例一:限制輸入只能是數字

利用 isNaN 判斷輸入是否為數值,若不是就執行 undo 把剛才的輸入還原:

<input type="text" name="text"
       onkeyup="if(isNaN(value)) execCommand('undo')" />

這個技巧允許小數點,因此 1981.121 這類輸入可以通過。不過要注意 isNaN('') 為 false,空值不會被攔下來。

應用範例二:一組編輯工具列

<input type="button" value="剪切"   onclick="document.execCommand('Cut')">
<input type="button" value="拷貝"   onclick="document.execCommand('Copy')">
<input type="button" value="粘貼"   onclick="document.execCommand('Paste')">
<input type="button" value="撤消"   onclick="document.execCommand('Undo')">
<input type="button" value="重做"   onclick="document.execCommand('Redo')"
       id="button2" name="button2">>
<input>
<input type="button" value="刪除"   onclick="document.execCommand('Delete')">
<input type="button" value="黑體"   onclick="document.execCommand('Bold')">
<input type="button" value="斜體"   onclick="document.execCommand('Italic')">
<input type="button" value="下劃線" onclick="document.execCommand('Underline')">
<input type="button" value="停止"   onclick="document.execCommand('stop')">
<input type="button" value="保存"   onclick="document.execCommand('SaveAs')">
<input type="button" value="另存為" onclick="document.execCommand('Saveas', false, 'c:test.htm')">
<input type="button" value="字型"     onclick="document.execCommand('FontName', false, fn)">
<input type="button" value="字型大小" onclick="document.execCommand('FontSize', false, fs)">
<input type="button" value="重新整理" onclick="document.execCommand('refresh', false, 0)">

現代開發該怎麼辦

execCommand 已被 MDN 明確標示為 deprecated,各瀏覽器的行為不一致,Firefox 長期支援不佳,Paste、SaveAs 這類涉及本機檔案與剪貼簿的指令更因安全性考量而多被禁用。

實務上的建議是:

  • 需要編輯器功能時,採用 Slate、ProseMirror、Lexical、TipTap 這類以 Contenteditable 為基礎並自行管理狀態的成熟方案。
  • 需要複製到剪貼簿時,改用 navigator.clipboard.writeText()。
  • 需要唯讀數字輸入時,直接用 <input type="number"> 或加上 pattern 與驗證邏輯,比攔截 keyup 再 undo 可靠得多。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved