DIV CSS 佈局教程網

document.execCommand() 指令大全與實際用法
編輯:JavaScript基礎知識     

document.execCommand() 曾是實作網頁所見即所得編輯器的核心 API,它把大量編輯行為(粗體、字型、顏色、插入物件、復原等)收斂成一行呼叫。以下把常用指令依功能分類整理,並附上可運行的完整範例。

版面與定位類

  • D-Position:允許透過拖曳移動絕對定位的物件。
  • AbsolutePosition:將元素的 position 屬性設為 absolute。
  • JustifyLeft / JustifyCenter / JustifyRight:將選取區塊靠左、置中、靠右對齊。
  • Indent / Outdent:增加或減少選取區塊的縮進。
  • FormatBlock:設定目前的區塊格式化標籤。
  • LiveResize:迫使 MSHTML 編輯器在縮放或移動過程中持續更新外觀,而非等到動作結束才更新。

文字樣式類

  • Bold / Italic / Underline:切換粗體、斜體、底線。
  • FontName / FontSize:設定或取得選取區的字型與字級。
  • ForeColor / BackColor:設定或取得前景(文字)色與背景色。
  • StrikeThrough / SuperScript / SubScript:刪除線、上標、下標。
  • RemoveFormat:清除選取區的格式化標籤。
  • OverWrite:切換插入與覆寫狀態。

剪貼簿與歷史類

  • Copy / Cut / Paste:複製、剪下(複製並刪除)、貼上。
  • Delete:刪除選取區。
  • SelectAll / Unselect:全選文件、取消選取狀態。
  • Undo / Redo:復原與重做,IE5.0 之後支援近乎無限層級的復原。
  • Print:開啟列印對話框。
  • SaveAs:將當前頁面另存為檔案。
  • Refresh / Stop:重新整理當前文件、停止載入。

超連結與書籤類

  • CreateLink:為選取區插入超連結,或彈出對話框讓使用者輸入 URL。
  • Unlink:移除選取區內的所有超連結。
  • CreateBookmark / UnBookmark:建立或刪除書籤錨點。

插入物件類

  • InsertImage:插入圖片。
  • InsertHorizontalRule:插入水平線。
  • InsertParagraph:插入換行。
  • InsertOrderedList / InsertUnorderedList:切換為編號清單或項目符號清單。
  • InsertIFrame / InsertMarquee / InsertFieldset:插入內嵌框架、跑馬燈、方框。
  • InsertInputText / Password / Checkbox / Radio / Reset / Submit / Button / FileUpload / Hidden / Image:插入各類表單控制項。
  • InsertSelectDropdown / InsertSelectListbox / InsertTextArea:插入下拉選單、清單框、多行文字輸入框。

其他

  • MultipleSelection:允許使用者按住 Shift 或 Ctrl 一次選取多個元素。
  • Open:開啟。
  • 標註為「目前尚未支援」的指令(如 BlockDirLTR、BrowseMode、JustifyFull、SizeToControl 等)在實作上可以略過。

完整範例

<HTML>
<HEAD>
<TITLE>JavaScript--execCommand 指令集</TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
/* 執行 copy 指令 */
function fn_doufucopy(){
  edit.select();
  document.execCommand('Copy');
}
/* 執行 paste 指令 */
function fn_doufupaste() {
  tt.focus();
  document.execCommand('paste');
}
/* 建立一個超連結 */
function fn_creatlink()
{
  document.execCommand('CreateLink',true,'true');// 彈出對話框輸入 URL
  //document.execCommand('CreateLink',false,'https://www.51js.com');
}
/* 將選取區塊設為指定背景色 */
function fn_change_backcolor()
{
  document.execCommand('BackColor',true,'#FFbbDD');
}
/* 改變前景色、字級、字型、粗體與斜體 */
function fn_change_forecolor()
{
  document.execCommand('ForeColor',false,'#BBDDCC');
  document.execCommand('FontSize',false,7);
  document.execCommand('FontName',false,'標楷體');
  document.execCommand('Bold');
  document.execCommand('Italic');
}
/* 為選取區塊加上不同線條 */
function fn_change_selection()
{
  document.execCommand('Underline');
  document.execCommand('StrikeThrough');
  document.execCommand('SuperScript');
  document.execCommand('Underline');
}
/* 將選取區塊排成不同格式 */
function fn_format()
{
  document.execCommand('InsertOrderedList');
  document.execCommand('InsertUnorderedList');
  document.execCommand('Indent');
}
/* 剪下或刪除選取區塊 */
function fn_CutOrDel()
{
  //document.execCommand('Delete');
  document.execCommand('Cut');
}
/* 將選取區塊重設為指定的物件 */
function fn_InsObj()
{
  /* 以下指令皆為選取區塊重設一個 object;
     第二個參數 true 或 false 效果相同;
     第三個參數為該 object 的 id,可日後用 JS 取得控制 */
  document.execCommand('InsertSelectDropdown',true,"aa");
  //document.execCommand('InsertButton',false,"aa");
  //document.execCommand('InsertFieldSet',true,"aa");
  //document.execCommand('InsertHorizontalRule',true,"aa");
  //document.execCommand('InsertIFrame',true,"aa");
  //document.execCommand('InsertImage',false,"aa");
  //document.execCommand('InsertInputCheckbox',true,"aa");
  //document.execCommand('InsertTextArea',true,"aa");
  //document.execCommand('Unselect');
  //document.execCommand('SelectAll');
}
/* 將頁面保存為檔案或列印 */
function fn_save()
{
  document.execCommand('SaveAs','mycodes.txt');
  //document.execCommand('print');
}
-->
</SCRIPT>
</HEAD>
<body>
<input id="edit" value="范例" NAME="edit"><br>
<button onclick="fn_doufucopy()" ID="Button1">Copy</button>
<button onclick="fn_doufupaste()" ID="Button2">paste</button><br>
<textarea id="tt" rows="10" cols="50" NAME="tt"></textarea>
<hr><br>
Please select above letters, then click following buttons:<br>
<hr>
<input type="button" value="創建 CreateLink" onclick="fn_creatlink()"><br>
<input type="button" value="改變文字背景色" onclick="fn_change_backcolor()"><br>
<input type="button" value="改變文字前景色" onclick="fn_change_forecolor()"><br>
<input type="button" value="給文字加線條" onclick="fn_change_selection()"><br>
<input type="button" value="改變文字的排列" onclick="fn_format()"><br>
<input type="button" value="刪除或剪下選中的部分" onclick="fn_CutOrDel()"><br>
<input type="button" value="插入 Object" onclick="fn_InsObj()"><br>
<input type="button" value="保存或列印檔案" onclick="fn_save()"><br>
<input type="button" value="測試 Refresh 屬性" onclick="document.execCommand('Refresh')">
</body>
</HTML>

在 iframe 中實作編輯區

更常見的做法是先啟用一個 iframe 的編輯狀態,再對它的 document 下指令:

IframeName.document.designMode = "On"

此後常用的指令可以濃縮成下面這張對照表:

字型     execCommand("fontname", "", 字體)
字級     execCommand("fontsize", "", 字級)
粗體     execCommand("Bold")
斜體     execCommand("Italic")
底線     execCommand("Underline")
刪除線   execCommand("StrikeThrough")
上標     execCommand("SuperScript")
下標     execCommand("SubScript")
編號清單 execCommand("InsertOrderedList")
項目清單 execCommand("InsertUnorderedList")
向外縮進 execCommand("Outdent")
向內縮進 execCommand("Indent")
靠左     execCommand("JustifyLeft")
靠右     execCommand("JustifyRight")
置中     execCommand("JustifyCenter")
剪下     execCommand("Cut")
複製     execCommand("Copy")
貼上     execCommand("Paste")
覆寫     execCommand("Overwrite")
復原     execCommand("Undo")
重做     execCommand("Redo")
字色     execCommand("ForeColor", "", CColor)
插入圖片 execCommand("InsertImage", "", ImgURL)

補充兩點歷史細節:CreateLink 在 IE5.0 以後會開啟內建視窗,可直接選擇 MAILTO、FTP 等連結類型,較為便利;IE4.0 沒有這個內建視窗,必須用 execCommand("CreateLink","",TURL) 的形式自行嵌入。另外,IE 中嵌入的可編輯控制項預設面向本地資源,因此基於 Web 的圖片插入最好自己做一個輸入框接收網址,再呼叫 InsertImage。

現況提醒

execCommand 已被標示為不建議使用(deprecated),各瀏覽器的行為差異大、對 contenteditable 的支援也不一致。新專案若需要富文字編輯,建議改用 contenteditable 搭配 Selection / Range API,或直接採用成熟的編輯器框架;而剪貼簿操作則應改用非同步的 navigator.clipboard API。

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