execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用
如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的”2D-Position”),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的”false”即表示不显示对话框),动态参数一
般为一可用值或属性值(如下例中的”true”)。
document.execCommand("2D-Position","false","true"); 下面列出的是指令参数及意义
document.ExecCommand() – html实例
1 <HTML>
2
3 <HEAD>
4
5 <TITLE>JavaScript--execCommand指令集</TITLE>
6
7 <SCRIPT LANGUAGE="javascript">
8
9 <!--
10
11 /*
12
13 *该function执行copy指令
14
15 */
16
17 function fn_doufucopy(){
18
19 edit.select();
20
21 document.execCommand('Copy');
22
23 }
24
25 /*
26
27 *该function执行paste指令
28
29 */
30
31 function fn_doufupaste() {
32
33 tt.focus();
34
35 document.execCommand('paste');
36
37 }
38
39 /*
40
41 *该function用来创建一个超链接
42
43 */
44
45 function fn_creatlink()
46
47 {
48
49 document.execCommand('CreateLink',true,'true');//弹出一个对话框输入URL
50
51 //document.execCommand('CreateLink',false,'http://www.51js.com');
52
53 }
54
55 /*
56
57 *该function用来将选中的区块设为指定的背景色
58
59 */
60
61 function fn_change_backcolor()
62
63 {
64
65 document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以
66
67 }
68
69 /*
70
71 *该function用来将选中的区块设为指定的前景色,改变选中区块的字体大小,改变字体,字体变粗变斜
72
73 */
74
75 function fn_change_forecolor()
76
77 {
78
79 //指定前景色
80
81 document.execCommand('ForeColor',false,'#BBDDCC');//true或false都可以
82
83 //指定背景色
84
85 document.execCommand('FontSize',false,7); //true或false都可以
86
87 //字体必须是系统支持的字体
88
89 document.execCommand('FontName',false,'标楷体'); //true或false都可以
90
91 //字体变粗
92
93 document.execCommand('Bold');
94
95 //变斜体
96
97 document.execCommand('Italic');
98
99 }
100
101 /*
102
103 *该function用来将选中的区块加上不同的线条
104
105 */
106
107 function fn_change_selection()
108
109 {
110
111 //将选中的文字加下划线
112
113 document.execCommand('Underline');
114
115 //在选中的文字上划粗线
116
117 document.execCommand('StrikeThrough');
118
119 //将选中的部分文字变细
120
121 document.execCommand('SuperScript');
122
123 //将选中区块的下划线取消掉
124
125 document.execCommand('Underline');
126
127 }
128
129 /*
130
131 *该function用来将选中的区块排成不同的格式
132
133 */
134
135 function fn_format()
136
137 {
138
139 //有序列排列
140
141 document.execCommand('InsertOrderedList');
142
143 //实心无序列排列
144
145 document.execCommand('InsertUnorderedList');
146
147 //空心无序列排列
148
149 document.execCommand('Indent');
150
151 }
152
153 /*
154
155 *该function用来将选中的区块剪下或是删除掉
156
157 */
158
159 function fn_CutOrDel()
160
161 {
162
163 //删除选中的区块
164
165 //document.execCommand('Delete');
166
167 //剪下选中的区块
168
169 document.execCommand('Cut');
170
171 }
172
173 /*
174
175 *该function用来将选中的区块重设为一个相应的物件
176
177 */
178
179 function fn_InsObj()
180
181 {
182
183 /*
184
185 ******************************************
186
187 * 以下指令都是为选中的区块重设一个object;
188
189 * 如没有特殊说明,第二个参数true或false是一样的;
190
191 * 参数三表示为该object的id;
192
193 * 可以用在javascript中通过其指定的id来控制它
194
195 ******************************************
196
197 */
198
199 /*重设为一个button(InsertButton和InsertInputButtong一样,
200
201 只不前者是button,后者是input)*/
202
203 /*document.execCommand('InsertButton',false,"aa"); //true或false无效
204
205 document.all.aa.value="风舞九天";*/
206
207 //重设为一个fieldset
208
209 /*document.execCommand('InsertFieldSet',true,"aa");
210
211 document.all.aa.innerText="刀剑如梦";*/
212
213 //插入一个水平线
214
215 //document.execCommand('InsertHorizontalRule',true,"aa");
216
217 //插入一个iframe
218
219 //document.execCommand('InsertIFrame',true,"aa");
220
221 //插入一个InsertImage,设为true时需要图片,false时不需图片
222
223 //document.execCommand('InsertImage',false,"aa");
224
225 //插入一个checkbox
226
227 //document.execCommand('InsertInputCheckbox',true,"aa");
228
229 //插入一个file类型的object
230
231 //document.execCommand('InsertInputFileUpload',false,"aa");
232
233 //插入一个hidden
234
235 /*document.execCommand('InsertInputHidden',false,"aa");
236
237 alert(document.all.aa.id);*/
238
239 //插入一个InputImage
240
241 /*document.execCommand('InsertInputImage',false,"aa");
242
243 document.all.aa.src="F-a10.gif";*/
244
245 //插入一个Password
246
247 //document.execCommand('InsertInputPassword',true,"aa");
248
249 //插入一个Radio
250
251 //document.execCommand('InsertInputRadio',false,"aa");
252
253 //插入一个Reset
254
255 //document.execCommand('InsertInputReset',true,"aa");
256
257 //插入一个Submit
258
259 //document.execCommand('InsertInputSubmit',false,"aa");
260
261 //插入一个input text
262
263 //document.execCommand('InsertInputText',false,"aa");
264
265 //插入一个textarea
266
267 //document.execCommand('InsertTextArea',true,"aa");
268
269 //插入一个 select list box
270
271 //document.execCommand('InsertSelectListbox',false,"aa");
272
273 //插入一个single select
274
275 document.execCommand('InsertSelectDropdown',true,"aa");
276
277 //插入一个line break(硬回车??)
278
279 //document.execCommand('InsertParagraph');
280
281 //插入一个marquee
282
283 /*document.execCommand('InsertMarquee',true,"aa");
284
285 document.all.aa.innerText="bbbbb";*/
286
287 //用于取消选中的阴影部分
288
289 //document.execCommand('Unselect');
290
291 //选中页面上的所有元素
292
293 //document.execCommand('SelectAll');
294
295 }
296
297 /*
298
299 *该function用来将页面保存为一个文件
300
301 */
302
303 function fn_save()
304
305 {
306
307 //第二个参数为欲保存的文件名
308
309 document.execCommand('SaveAs','mycodes.txt');
310
311 //打印整个页面
312
313 //document.execCommand('print');
314
315 }
316
317 -->
318
319 </SCRIPT>
320
321 </HEAD>
322
323 <body>
324
325 <input id="edit" value="范例" NAME="edit"><br>
326
327 <button onclick="fn_doufucopy()" ID="Button1">Copy</button> <button onclick="fn_doufupaste()" ID="Button2">
328
329 paste</button><br>
330
331 <textarea id="tt" rows="10" cols="50" NAME="tt"></textarea>
332
333 <hr>
334
335 <br>
336
337 浮沉聚散变化又再,但是总可卷土重来.<br>
338
339 天若有情天亦老,人间正道是沧桑.<br>
340
341 都怪我,太执着,却也等不到花开叶落.<br>
342
343 <br>
344
345 Please select above letters, then click following buttons:<br>
346
347 <hr>
348
349 <input type="button" value="创建CreateLink" onclick="fn_creatlink()" ID="Button3" NAME="Button3"><br>
350
351 <input type="button" value="改变文字背景色" onclick="fn_change_backcolor()" ID="Button4" NAME="Button4"><br>
352
353 <input type="button" value="改变文字前景色" onclick="fn_change_forecolor()" ID="Button5" NAME="Button5"><br>
354
355 <input type="button" value="给文字加线条" onclick="fn_change_selection()" ID="Button6" NAME="Button6"><br>
356
357 <input type="button" value="改变文字的排列" onclick="fn_format()" ID="Button7" NAME="Button7"><br>
358
359 <input type="button" value="删除或剪下选中的部分" onclick="fn_CutOrDel()" ID="Button8" NAME="Button8"><br>
360
361 <input type="button" value="插入Object" onclick="fn_InsObj()" ID="Button9" NAME="Button9"><br>
362
363 <input type="button" value="保存或打印文件" onclick="fn_save()" ID="Button10" NAME="Button10"><br>
364
365 <input type="button" value="测试Refresh属性" onclick="document.execCommand('Refresh')" ID="Button11"
366
367 NAME="Button11">
368
369 </body>
370
371 </HTML>