DIV CSS 佈局教程網

document.execCommand() 指令大全與實際應用(含 Firefox 支援度提醒)
編輯:關於JavaScript     

開宗明義:這個 API 在 Firefox 下支援度不佳,且已被標示為 Deprecated。它源自 IE 的 MSHTML 編輯器,主要用途是操作 contenteditable 區域的內容。新專案請改用 document.execCommand 的替代方案(如 Selection API 搭配 Range,或直接使用 Slate、ProseMirror、Quill 等編輯器框架)。以下整理供維護舊系統參考。

排版與選取類

  • 2D-Position:允許透過拖曳移動絕對定位的物件。
  • AbsolutePosition:將元素的 position 設為 absolute。
  • SelectAll:選取整個文件。
  • Unselect:清除選取狀態。
  • MultipleSelection:允許按住 Shift 或 Ctrl 一次選取多個元素。
  • OverWrite:切換文字輸入的插入與覆寫狀態。
  • LiveResize:迫使編輯器在縮放或移動過程中持續更新元素外觀。

文字樣式類

  • Bold / Italic / Underline:切換粗體、斜體、底線。
  • FontName / FontSize:設定或取得選取範圍的字體與字級。
  • ForeColor / BackColor:設定前景(文字)色與背景色。
  • StrikeThrough:刪除線。
  • Subscript / Superscript:下標/上標。
  • RemoveFormat:移除選取範圍的格式化標籤。
  • RemoveParaFormat:移除段落格式化。

段落與對齊類

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

插入物件類

  • InsertImage:插入圖片。
  • InsertLink / CreateLink / Unlink:插入或移除超連結。
  • InsertBookmark / CreateBookmark / UnBookmark:書籤相關操作。
  • InsertIFrame:插入內嵌框架。
  • InsertMarquee:插入跑馬燈。
  • InsertButton、InsertInputButton、InsertInputCheckbox、InsertInputFileUpload、InsertInputHidden、InsertInputImage、InsertInputPassword、InsertInputRadio、InsertInputReset、InsertInputSubmit、InsertInputText:以各種表單控制項覆蓋選取範圍。
  • InsertFieldset:以方框覆蓋選取範圍。
  • InsertSelectDropdown / InsertSelectListbox:插入下拉框/列表框。
  • InsertTextArea:插入多行文字輸入控制項。

剪貼簿與歷史類

  • Cut / Copy / Paste:剪下、複製、貼上。
  • Delete:刪除選取範圍。
  • Undo / Redo:復原、重做。
  • Refresh:重新整理目前文件。
  • Stop:停止。
  • Open:開啟。
  • Print:開啟列印對話框。
  • SaveAs:將目前網頁另存為檔案。

尚未支援的指令

BlockDirLTR、BlockDirRTL、BrowseMode、DirLTR、DirRTL、EditMode、InlineDirLTR、InlineDirRTL、PlayImage、SizeToControl、SizeToControlHeight、SizeToControlWidth、StopImage 目前皆未支援。

HTA 專用

dialogHide、edge、unadorned 等屬性只在 HTML Application 中有意義,一般網頁不會用到。

實際範例

例 1:限制輸入只能是數字

isNaN 用來測試是否為非數值型。搭配 execCommand('undo') 可以讓非法輸入自動被復原,形成簡單的輸入過濾:

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

注意這個寫法允許 1981.121 這類值,最多一個小數點。它只能擋掉「非數字」輸入,並非完整的表單驗證。

例 2:一組編輯工具列

<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')">
<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)">

其中 Copy、Cut、Paste、SaveAs 涉及本機檔案與剪貼簿,現代瀏覽器基於安全考量都需要使用者手勢觸發,且部分已在非 IE 環境中移除,請勿依賴。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved