---
title: "JavaScript的execCommand指令集"
author: "小杨学JAVA"
date: "2014-01-15T10:26:38Z"
tags: []
categories: ["Web前端"]
slug: "javascript-execcommand"
draft: false
lastmod: "2014-01-15T10:26:38Z"
id: 2004192
url: "https://z3sm2012.iteye.com/blog/2004192"
---

# JavaScript的execCommand指令集

 

001. /*
002. \*该function执行copy指令
003. \*/
004. function fn\_doufucopy(){
005. edit.select();
006. document.execCommand('Copy');
007. }
008. /*
009. \*该function执行paste指令
010. \*/
011. function fn\_doufupaste() {
012. tt.focus();
013. document.execCommand('paste');
014. }
015. /*
016. \*该function用来创建一个超链接
017. \*/
018. function fn\_creatlink()
019. {
020. document.execCommand('CreateLink',true,'true');//弹出一个对话框输入URL
021. //document.execCommand('CreateLink',false,'http://www.51js.com');
022. }
023. /*
024. \*该function用来将选中的区块设为指定的背景色
025. \*/
026. function fn\_change\_backcolor()
027. {
028. document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以
029. }
030. /*
031. \*该function用来将选中的区块设为指定的前景色,改变选中区块的字体大小,改变字体,字体变粗变斜
032. \*/
033. function fn\_change\_forecolor()
034. {
035. //指定前景色
036. document.execCommand('ForeColor',false,'#BBDDCC');//true或false都可以
037. //指定背景色
038. document.execCommand('FontSize',false,7);   //true或false都可以
039. //字体必须是系统支持的字体
040. document.execCommand('FontName',false,'标楷体');   //true或false都可以
041. //字体变粗
042. document.execCommand('Bold');
043. //变斜体
044. document.execCommand('Italic');
045. }
046. /*
047. \*该function用来将选中的区块加上不同的线条
048. \*/
049. function fn\_change\_selection()
050. {
051. //将选中的文字加下划线
052. document.execCommand('Underline');
053. //在选中的文字上划粗线
054. document.execCommand('StrikeThrough');
055. //将选中的部分文字变细
056. document.execCommand('SuperScript');
057. //将选中区块的下划线取消掉
058. document.execCommand('Underline');
059. }
060. /*
061. \*该function用来将选中的区块排成不同的格式
062. \*/
063. function fn\_format()
064. {
065. //有序列排列
066. document.execCommand('InsertOrderedList');
067. //实心无序列排列
068. document.execCommand('InsertUnorderedList');
069. //空心无序列排列
070. document.execCommand('Indent');
071. }
072. /*
073. \*该function用来将选中的区块剪下或是删除掉
074. \*/
075. function fn\_CutOrDel()
076. {
077. //删除选中的区块
078. //document.execCommand('Delete');
079. //剪下选中的区块
080. document.execCommand('Cut');
081. }
082. /*
083. \*该function用来将选中的区块重设为一个相应的物件
084. \*/
085. function fn\_InsObj()
086. {
087. /*
088. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
089. * 以下指令都是为选中的区块重设一个object;
090. * 如没有特殊说明,第二个参数true或false是一样的;
091. * 参数三表示为该object的id;
092. * 可以用在javascript中通过其指定的id来控制它
093. \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**
094. \*/
095. /\*重设为一个button(InsertButton和InsertInputButtong一样,
096. 只不前者是button,后者是input)\*/
097. /\*document.execCommand('InsertButton',false,"aa"); //true或false无效
098. document.all.aa.value="风舞九天";\*/
099. //重设为一个fieldset
100. /\*document.execCommand('InsertFieldSet',true,"aa");
101. document.all.aa.innerText="刀剑如梦";\*/
102. //插入一个水平线
103. //document.execCommand('InsertHorizontalRule',true,"aa");
104. //插入一个iframe
105. //document.execCommand('InsertIFrame',true,"aa");
106. //插入一个InsertImage,设为true时需要图片,false时不需图片
107. //document.execCommand('InsertImage',false,"aa");
108. //插入一个checkbox
109. //document.execCommand('InsertInputCheckbox',true,"aa");
110. //插入一个file类型的object
111. //document.execCommand('InsertInputFileUpload',false,"aa");
112. //插入一个hidden
113. /\*document.execCommand('InsertInputHidden',false,"aa");
114. alert(document.all.aa.id);\*/
115. //插入一个InputImage
116. /\*document.execCommand('InsertInputImage',false,"aa");
117. document.all.aa.src="F-a10.gif";\*/
118. //插入一个Password
119. //document.execCommand('InsertInputPassword',true,"aa");
120. //插入一个Radio
121. //document.execCommand('InsertInputRadio',false,"aa");
122. //插入一个Reset
123. //document.execCommand('InsertInputReset',true,"aa");
124. //插入一个Submit
125. //document.execCommand('InsertInputSubmit',false,"aa");
126. //插入一个input text
127. //document.execCommand('InsertInputText',false,"aa");
128. //插入一个textarea
129. //document.execCommand('InsertTextArea',true,"aa");
130. //插入一个 select list box
131. //document.execCommand('InsertSelectListbox',false,"aa");
132. //插入一个single select
133. //document.execCommand('InsertSelectDropdown',true,"aa");
134. //插入一个line break(硬回车??)
135. //document.execCommand('InsertParagraph');
136. //插入一个marquee
137. /\*document.execCommand('InsertMarquee',true,"aa");
138. document.all.aa.innerText="bbbbb";\*/
139. //用于取消选中的阴影部分
140. //document.execCommand('Unselect');
141. //选中页面上的所有元素
142. //document.execCommand('SelectAll');
143. }
144. /*
145. \*该function用来将页面保存为一个文件
146. \*/
147. function fn\_save()
148. {
149. //第二个参数为欲保存的文件名
150. document.execCommand('SaveAs','mycodes.txt');
151. //打印整个页面
152. //document.execCommand('print');

 

转：http://www.cnblogs.com/top5/archive/2011/07/04/2097809.html