JavaScript中的execCommand

execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用

如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的”2D-Position”),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的”false”即表示不显示对话框),动态参数一

般为一可用值或属性值(如下例中的”true”)。

 

document.execCommand("2D-Position","false","true"); 下面列出的是指令参数及意义

 

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

原文链接

转载于:https://www.cnblogs.com/spirit-ling/p/7043643.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值