JS:document.execCommand()解析

execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的"2D-Position"),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的"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 清除当前选中区的选中状态。

 

代码
1 <HTML>
2
3  <HEAD>
4
5  <TITLE>JavaScript--execCommand指令集</TITLE>
6
7  <SCRIPT LANGUAGE="javascript">
8
9 <!--
10
11 function fn_doufucopy(){
12
13 edit.select();
14
15 document.execCommand('Copy');
16
17 }
18
19 function fn_doufupaste() {
20
21 tt.focus();
22
23 document.execCommand('paste');
24
25 }
26
27 function fn_creatlink()
28
29 {
30
31 document.execCommand('CreateLink',true,'true');//弹出一个对话框输入URL
32
33 //document.execCommand('CreateLink',false,'http://www.51js.com');
34
35 }
36
37 function fn_change_backcolor()
38
39 {
40
41 document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以
42
43 }
44
45 function fn_change_forecolor()
46
47 {
48
49 //指定前景色
50
51 document.execCommand('ForeColor',false,'#BBDDCC');//true或false都可以
52
53 //指定背景色
54
55 document.execCommand('FontSize',false,7); //true或false都可以
56
57 //字体必须是系统支持的字体
58
59 document.execCommand('FontName',false,'标楷体'); //true或false都可以
60
61 //字体变粗
62
63 document.execCommand('Bold');
64
65 //变斜体
66
67 document.execCommand('Italic');
68
69 }
70
71 function fn_change_selection()
72
73 {
74
75 //将选中的文字加下划线
76
77 document.execCommand('Underline');
78
79 //在选中的文字上划粗线
80
81 document.execCommand('StrikeThrough');
82
83 //将选中的部分文字变细
84
85 document.execCommand('SuperScript');
86
87 //将选中区块的下划线取消掉
88
89 document.execCommand('Underline');
90
91 }
92
93 function fn_format()
94
95 {
96
97 //有序列排列
98
99 document.execCommand('InsertOrderedList');
100
101 //实心无序列排列
102
103 document.execCommand('InsertUnorderedList');
104
105 //空心无序列排列
106
107 document.execCommand('Indent');
108
109 }
110
111 function fn_CutOrDel()
112
113 {
114
115 //删除选中的区块
116
117 //document.execCommand('Delete');
118
119 //剪下选中的区块
120
121 document.execCommand('Cut');
122
123 }
124
125 function fn_InsObj()
126
127 {
128
129 //重设为一个fieldset
130
131 //插入一个水平线
132
133 //document.execCommand('InsertHorizontalRule',true,"aa");
134
135 //插入一个iframe
136
137 //document.execCommand('InsertIFrame',true,"aa");
138
139 //插入一个InsertImage,设为true时需要图片,false时不需图片
140
141 //document.execCommand('InsertImage',false,"aa");
142
143 //插入一个checkbox
144
145 //document.execCommand('InsertInputCheckbox',true,"aa");
146
147 //插入一个file类型的object
148
149 //document.execCommand('InsertInputFileUpload',false,"aa");
150
151 //插入一个hidden
152
153 //插入一个InputImage
154
155 //插入一个Password
156
157 //document.execCommand('InsertInputPassword',true,"aa");
158
159 //插入一个Radio
160
161 //document.execCommand('InsertInputRadio',false,"aa");
162
163 //插入一个Reset
164
165 //document.execCommand('InsertInputReset',true,"aa");
166
167 //插入一个Submit
168
169 //document.execCommand('InsertInputSubmit',false,"aa");
170
171 //插入一个input text
172
173 //document.execCommand('InsertInputText',false,"aa");
174
175 //插入一个textarea
176
177 //document.execCommand('InsertTextArea',true,"aa");
178
179 //插入一个 select list box
180
181 //document.execCommand('InsertSelectListbox',false,"aa");
182
183 //插入一个single select
184
185 document.execCommand('InsertSelectDropdown',true,"aa");
186
187 //插入一个line break(硬回车??)
188
189 //document.execCommand('InsertParagraph');
190
191 //插入一个marquee
192
193 //用于取消选中的阴影部分
194
195 //document.execCommand('Unselect');
196
197 //选中页面上的所有元素
198
199 //document.execCommand('SelectAll');
200
201 }
202
203 function fn_save()
204
205 {
206
207 //第二个参数为欲保存的文件名
208
209 document.execCommand('SaveAs','mycodes.txt');
210
211 //打印整个页面
212
213 //document.execCommand('print');
214
215 }
216
217 -->
218
219 </SCRIPT>
220
221 </HEAD>
222
223 <body>
224
225 <input id="edit" value="范例" NAME="edit"><br>
226
227 <button onclick="fn_doufucopy()" ID="Button1">Copy</button> <button onclick="fn_doufupaste()" ID="Button2">
228
229 paste</button><br>
230
231 <textarea id="tt" rows="10" cols="50" NAME="tt"></textarea>
232
233 <hr>
234
235 <br>
236
237 浮沉聚散变化又再,但是总可卷土重来.<br>
238
239 天若有情天亦老,人间正道是沧桑.<br>
240
241 都怪我,太执着,却也等不到花开叶落.<br>
242
243 <br>
244
245 Please select above letters, then click following buttons:<br>
246
247 <hr>
248
249 <input type="button" value="创建CreateLink" onclick="fn_creatlink()" ID="Button3" NAME="Button3"><br>
250
251 <input type="button" value="改变文字背景色" onclick="fn_change_backcolor()" ID="Button4" NAME="Button4"><br>
252
253 <input type="button" value="改变文字前景色" onclick="fn_change_forecolor()" ID="Button5" NAME="Button5"><br>
254
255 <input type="button" value="给文字加线条" onclick="fn_change_selection()" ID="Button6" NAME="Button6"><br>
256
257 <input type="button" value="改变文字的排列" onclick="fn_format()" ID="Button7" NAME="Button7"><br>
258
259 <input type="button" value="删除或剪下选中的部分" onclick="fn_CutOrDel()" ID="Button8" NAME="Button8"><br>
260
261 <input type="button" value="插入Object" onclick="fn_InsObj()" ID="Button9" NAME="Button9"><br>
262
263 <input type="button" value="保存或打印文件" onclick="fn_save()" ID="Button10" NAME="Button10"><br>
264
265 <input type="button" value="测试Refresh属性" onclick="document.execCommand('Refresh')" ID="Button11"
266
267 NAME="Button11">
268
269 </body>
270
271 </HTML>
272

 

 

普通的方式是激活一个<iframe>进入编辑状态,命令如下

IframeNamer.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")

取消操作--IE5.0以后可以无限取消

execCommand("Undo")

重复操作

execCommand("Redo")

设置链接--若按以下写法,在IE5.0版本中会激活一个内建窗口,可以完成输入链接的功能,而且还可以选择MAILTO、FTP等各种链接类型,比较方便

execCommand("CreateLink")

在IE4.0中,没有内建链接输入窗口,所以就需要用以下方式嵌入链接

execCommand("CreateLink","",TURL)

插入图片--由于IE中嵌入的可编控件是针对本地资源的,所以其默认的图片资源来自本地,所以基于WEB内容的编辑最好自己做输入框,然后用如下命令实现。

execCommand("InsertImage","",ImgURL)

字体颜色

execCommand("ForeColor","",CColor)

posted @ 2009-04-28 13:46  kk.1230  阅读(1236)  评论(0编辑  收藏  举报