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