execCommand方法是執行一個對當前文檔,當前選擇或者給出範圍的命令。處理Html資料時常用
如下格式:document.execCommand(sCommand[,互動方式, 動態參數]) ,其中:sCommand為指令參數(如下例中的”2D-Position”),互動方式參數如果是true的話將顯示對話方塊,如果為false的話,則不顯示對話方塊(下例中的”false”即表示不顯示對話方塊),動態參數一般為一可用值或屬性值(如下例中的”true”)。
document.execCommand("2D-Position","false","true");下面列出的是指令參數及意義2D-Position 允許通過拖曳移動絕對位置的對象。AbsolutePosition 設定元素的 position 屬性為“absolute”(絕對)。BackColor 設定或擷取當前選中區的背景顏色。BlockDirLTR 目前尚未支援。BlockDirRTL 目前尚未支援。Bold 切換當前選中區的粗體顯示與否。BrowseMode 目前尚未支援。Copy 將當前選中區複製到剪貼簿。CreateBookmark 建立一個書籤錨或擷取當前選中區或插入點的書籤錨的名稱。CreateLink 在當前選中區上插入超級連結,或顯示一個對話方塊允許使用者指定要為當前選中區插入的超級連結的 URL。Cut 將當前選中區複製到剪貼簿並刪除之。Delete 刪除當前選中區。DirLTR 目前尚未支援。DirRTL 目前尚未支援。EditMode 目前尚未支援。FontName 設定或擷取當前選中區的字型。FontSize 設定或擷取當前選中區的字型大小。ForeColor 設定或擷取當前選中區的前景(文本)顏色。FormatBlock 設定當前塊格式化標籤。Indent 增加選中文本的縮排。InlineDirLTR 目前尚未支援。InlineDirRTL 目前尚未支援。InsertButton 用按鈕控制項覆蓋當前選中區。InsertFieldset 用方框覆蓋當前選中區。InsertHorizontalRule 用水平線覆蓋當前選中區。InsertIFrame 用內嵌架構覆蓋當前選中區。InsertImage 用映像覆蓋當前選中區。InsertInputButton 用按鈕控制項覆蓋當前選中區。InsertInputCheckbox 用複選框控制項覆蓋當前選中區。InsertInputFileUpload 用檔案上傳控制項覆蓋當前選中區。InsertInputHidden 插入隱藏控制項覆蓋當前選中區。InsertInputImage 用影像控制覆蓋當前選中區。InsertInputPassword 用密碼控制項覆蓋當前選中區。InsertInputRadio 用單選鈕控制項覆蓋當前選中區。InsertInputReset 用重設控制項覆蓋當前選中區。InsertInputSubmit 用提交控制項覆蓋當前選中區。InsertInputText 用文本控制項覆蓋當前選中區。InsertMarquee 用空字幕覆蓋當前選中區。InsertOrderedList 切換當前選中區是編號清單還是常規格式化塊。InsertParagraph 用換行覆蓋當前選中區。InsertSelectDropdown 用下拉框控制項覆蓋當前選中區。InsertSelectListbox 用列表框控制項覆蓋當前選中區。InsertTextArea 用多行文本輸入控制項覆蓋當前選中區。InsertUnorderedList 切換當前選中區是項目符號清單還是常規格式化塊。Italic 切換當前選中區斜體顯示與否。JustifyCenter 將當前選中區在所在格式化塊置中。JustifyFull 目前尚未支援。JustifyLeft 將當前選中區所在格式化塊靠左對齊。JustifyNone 目前尚未支援。JustifyRight 將當前選中區所在格式化塊靠右對齊。LiveResize 迫使 MSHTML 編輯程式在縮放或移動過程中持續更新元素外觀,而不是只在移動或縮放完成後更新。MultipleSelection 允許當使用者按住 Shift 或 Ctrl 鍵時一次選中多於一個網站可選元素。Open 目前尚未支援。Outdent 減少選中區所在格式化塊的縮排。OverWrite 切換文本狀態的插入和覆蓋。Paste 用剪貼簿內容覆蓋當前選中區。PlayImage 目前尚未支援。Print 開啟列印對話方塊以便使用者可以列印當前頁。Redo 目前尚未支援。Refresh 重新整理當前文檔。RemoveFormat 從當前選中區中刪除格式化標籤。RemoveParaFormat 目前尚未支援。SaveAs 將當前 Web 頁面儲存為檔案。SelectAll 選中整個文檔。SizeToControl 目前尚未支援。SizeToControlHeight 目前尚未支援。SizeToControlWidth 目前尚未支援。Stop 目前尚未支援。StopImage 目前尚未支援。StrikeThrough 目前尚未支援。Subscript 目前尚未支援。Superscript 目前尚未支援。UnBookmark 從當前選中區中刪除全部書籤。Underline 切換當前選中區的底線顯示與否。Undo 目前尚未支援。Unlink 從當前選中區中刪除全部超級連結。Unselect 清除當前選中區的選中狀態。
document.ExecCommand() – html執行個體
<HTML> <HEAD> <TITLE>JavaScript--execCommand指令集</TITLE> <SCRIPT LANGUAGE="javascript"><!--/**該function執行copy指令*/function fn_doufucopy(){edit.select();document.execCommand('Copy');}/**該function執行paste指令*/function fn_doufupaste() { tt.focus();document.execCommand('paste');} /**該function用來建立一個超連結*/function fn_creatlink(){ document.execCommand('CreateLink',true,'true');//彈出一個對話方塊輸入URL //document.execCommand('CreateLink',false,'http://www.51js.com');}/**該function用來將選中的區塊設為指定的背景色*/function fn_change_backcolor(){ document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以}/**該function用來將選中的區塊設為指定的前景色彩,改變選中區塊的字型大小,改變字型,字型變粗變斜*/function fn_change_forecolor(){//指定前景色彩document.execCommand('ForeColor',false,'#BBDDCC');//true或false都可以//指定背景色document.execCommand('FontSize',false,7); //true或false都可以//字型必須是系統支援的字型document.execCommand('FontName',false,'標楷體'); //true或false都可以//字型變粗document.execCommand('Bold');//變斜體document.execCommand('Italic');}/**該function用來將選中的區塊加上不同的線條*/function fn_change_selection(){//將選中的文字加底線document.execCommand('Underline');//在選中的文字上劃粗線document.execCommand('StrikeThrough');//將選中的部分文字變細document.execCommand('SuperScript');//將選中區塊的底線取消掉document.execCommand('Underline'); }/* *該function用來將選中的區塊排成不同的格式 */function fn_format(){//有序列排列document.execCommand('InsertOrderedList');//實心無序列排列document.execCommand('InsertUnorderedList');//空心無序列排列document.execCommand('Indent');}/**該function用來將選中的區塊剪下或是刪除掉*/function fn_CutOrDel(){//刪除選中的區塊//document.execCommand('Delete');//剪下選中的區塊document.execCommand('Cut');}/**該function用來將選中的區塊重設為一個相應的物件*/function fn_InsObj(){/* ****************************************** * 以下指令都是為選中的區塊重設一個object; * 如沒有特殊說明,第二個參數true或false是一樣的; * 參數三表示為該object的id; * 可以用在javascript中通過其指定的id來控制它 *******************************************//*重設為一個button(InsertButton和InsertInputButtong一樣,只不前者是button,後者是input)*//*document.execCommand('InsertButton',false,"aa"); //true或false無效document.all.aa.value="風舞九天";*///重設為一個fieldset/*document.execCommand('InsertFieldSet',true,"aa");document.all.aa.innerText="刀劍如夢";*///插入一個水平線//document.execCommand('InsertHorizontalRule',true,"aa");//插入一個iframe//document.execCommand('InsertIFrame',true,"aa");//插入一個InsertImage,設為true時需要圖片,false時不需圖片//document.execCommand('InsertImage',false,"aa");//插入一個checkbox//document.execCommand('InsertInputCheckbox',true,"aa");//插入一個file類型的object//document.execCommand('InsertInputFileUpload',false,"aa");//插入一個hidden/*document.execCommand('InsertInputHidden',false,"aa");alert(document.all.aa.id);*///插入一個InputImage/*document.execCommand('InsertInputImage',false,"aa");document.all.aa.src="F-a10.gif";*///插入一個Password//document.execCommand('InsertInputPassword',true,"aa");//插入一個Radio//document.execCommand('InsertInputRadio',false,"aa");//插入一個Reset//document.execCommand('InsertInputReset',true,"aa");//插入一個Submit//document.execCommand('InsertInputSubmit',false,"aa");//插入一個input text//document.execCommand('InsertInputText',false,"aa");//插入一個textarea//document.execCommand('InsertTextArea',true,"aa");//插入一個 select list box//document.execCommand('InsertSelectListbox',false,"aa");//插入一個single selectdocument.execCommand('InsertSelectDropdown',true,"aa");//插入一個line break(硬斷行符號??)//document.execCommand('InsertParagraph');//插入一個marquee/*document.execCommand('InsertMarquee',true,"aa");document.all.aa.innerText="bbbbb";*///用於取消選中的陰影部分//document.execCommand('Unselect');//選中頁面上的所有元素//document.execCommand('SelectAll');}/**該function用來將頁面儲存為一個檔案*/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> 浮沉聚散變化又再,但是總可捲土重來.<br> 天若有情天亦老,人間正道是滄桑.<br> 都怪我,太執著,卻也等不到花開葉落.<br> <br> Please select above letters, then click following buttons:<br> <hr> <input type="button" value="建立CreateLink" onclick="fn_creatlink()" ID="Button3" NAME="Button3"><br> <input type="button" value="改變文字背景色" onclick="fn_change_backcolor()" ID="Button4" NAME="Button4"><br> <input type="button" value="改變文字前景色彩" onclick="fn_change_forecolor()" ID="Button5" NAME="Button5"><br> <input type="button" value="給文字加線條" onclick="fn_change_selection()" ID="Button6" NAME="Button6"><br> <input type="button" value="改變文字的排列" onclick="fn_format()" ID="Button7" NAME="Button7"><br> <input type="button" value="刪除或剪下選中的部分" onclick="fn_CutOrDel()" ID="Button8" NAME="Button8"><br> <input type="button" value="插入Object" onclick="fn_InsObj()" ID="Button9" NAME="Button9"><br> <input type="button" value="儲存或列印檔案" onclick="fn_save()" ID="Button10" NAME="Button10"><br> <input type="button" value="測試Refresh屬性" onclick="document.execCommand('Refresh')" ID="Button11" NAME="Button11"> </body></HTML>