http://digdeeply.org/archives/10191515.html
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用
如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的”2D-Position”),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的”false”即表示不显示对话框),动态参数一般为一可用值或属性值(如下例中的”true”)。
1 |
document.execCommand( "2D-Position" , "false" , "true" ); |
2 |
下面列出的是指令参数及意义 |
3 |
4 |
2D-Position 允许通过拖曳移动绝对定位的对象。 |
5 |
AbsolutePosition 设定元素的 position 属性为“absolute”(绝对)。 |
6 |
BackColor 设置或获取当前选中区的背景颜色。 |
7 |
BlockDirLTR 目前尚未支持。 |
8 |
BlockDirRTL 目前尚未支持。 |
9 |
Bold 切换当前选中区的粗体显示与否。 |
10 |
BrowseMode 目前尚未支持。 |
11 |
Copy 将当前选中区复制到剪贴板。 |
12 |
CreateBookmark 创建一个书签锚或获取当前选中区或插入点的书签锚的名称。 |
13 |
CreateLink 在当前选中区上插入超级链接,或显示一个对话框允许用户指定要为当前选中区插入的超级链接的 URL。 |
14 |
Cut 将当前选中区复制到剪贴板并删除之。 |
15 |
Delete 删除当前选中区。 |
16 |
DirLTR 目前尚未支持。 |
17 |
DirRTL 目前尚未支持。 |
18 |
EditMode 目前尚未支持。 |
19 |
FontName 设置或获取当前选中区的字体。 |
20 |
FontSize 设置或获取当前选中区的字体大小。 |
21 |
ForeColor 设置或获取当前选中区的前景(文本)颜色。 |
22 |
FormatBlock 设置当前块格式化标签。 |
23 |
Indent 增加选中文本的缩进。 |
24 |
InlineDirLTR 目前尚未支持。 |
25 |
InlineDirRTL 目前尚未支持。 |
26 |
InsertButton 用按钮控件覆盖当前选中区。 |
27 |
InsertFieldset 用方框覆盖当前选中区。 |
28 |
InsertHorizontalRule 用水平线覆盖当前选中区。 |
29 |
InsertIFrame 用内嵌框架覆盖当前选中区。 |
30 |
InsertImage 用图像覆盖当前选中区。 |
31 |
InsertInputButton 用按钮控件覆盖当前选中区。 |
32 |
InsertInputCheckbox 用复选框控件覆盖当前选中区。 |
33 |
InsertInputFileUpload 用文件上载控件覆盖当前选中区。 |
34 |
InsertInputHidden 插入隐藏控件覆盖当前选中区。 |
35 |
InsertInputImage 用图像控件覆盖当前选中区。 |
36 |
InsertInputPassword 用密码控件覆盖当前选中区。 |
37 |
InsertInputRadio 用单选钮控件覆盖当前选中区。 |
38 |
InsertInputReset 用重置控件覆盖当前选中区。 |
39 |
InsertInputSubmit 用提交控件覆盖当前选中区。 |
40 |
InsertInputText 用文本控件覆盖当前选中区。 |
41 |
InsertMarquee 用空字幕覆盖当前选中区。 |
42 |
InsertOrderedList 切换当前选中区是编号列表还是常规格式化块。 |
43 |
InsertParagraph 用换行覆盖当前选中区。 |
44 |
InsertSelectDropdown 用下拉框控件覆盖当前选中区。 |
45 |
InsertSelectListbox 用列表框控件覆盖当前选中区。 |
46 |
InsertTextArea 用多行文本输入控件覆盖当前选中区。 |
47 |
InsertUnorderedList 切换当前选中区是项目符号列表还是常规格式化块。 |
48 |
Italic 切换当前选中区斜体显示与否。 |
49 |
JustifyCenter 将当前选中区在所在格式化块置中。 |
50 |
JustifyFull 目前尚未支持。 |
51 |
JustifyLeft 将当前选中区所在格式化块左对齐。 |
52 |
JustifyNone 目前尚未支持。 |
53 |
JustifyRight 将当前选中区所在格式化块右对齐。 |
54 |
LiveResize 迫使 MSHTML 编辑器在缩放或移动过程中持续更新元素外观,而不是只在移动或缩放完成后更新。 |
55 |
MultipleSelection 允许当用户按住 Shift 或 Ctrl 键时一次选中多于一个站点可选元素。 |
56 |
Open 目前尚未支持。 |
57 |
Outdent 减少选中区所在格式化块的缩进。 |
58 |
OverWrite 切换文本状态的插入和覆盖。 |
59 |
Paste 用剪贴板内容覆盖当前选中区。 |
60 |
PlayImage 目前尚未支持。 |
61 |
Print 打开打印对话框以便用户可以打印当前页。 |
62 |
Redo 目前尚未支持。 |
63 |
Refresh 刷新当前文档。 |
64 |
RemoveFormat 从当前选中区中删除格式化标签。 |
65 |
RemoveParaFormat 目前尚未支持。 |
66 |
SaveAs 将当前 Web 页面保存为文件。 |
67 |
SelectAll 选中整个文档。 |
68 |
SizeToControl 目前尚未支持。 |
69 |
SizeToControlHeight 目前尚未支持。 |
70 |
SizeToControlWidth 目前尚未支持。 |
71 |
Stop 目前尚未支持。 |
72 |
StopImage 目前尚未支持。 |
73 |
StrikeThrough 目前尚未支持。 |
74 |
Subscript 目前尚未支持。 |
75 |
Superscript 目前尚未支持。 |
76 |
UnBookmark 从当前选中区中删除全部书签。 |
77 |
Underline 切换当前选中区的下划线显示与否。 |
78 |
Undo 目前尚未支持。 |
79 |
Unlink 从当前选中区中删除全部超级链接。 |
80 |
Unselect 清除当前选中区的选中状态。 |
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 > |