JavaScript中的execCommand
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用
如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的”2D-Position”),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的”false”即表示不显示对话框),动态参数一
般为一可用值或属性值(如下例中的”true”)。
document.execCommand("2D-Position","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 清除当前选中区的选中状态。
document.ExecCommand() – html实例
1 <HTML> 2 3 <HEAD> 4 5 <TITLE>JavaScript--execCommand指令集</TITLE> 6 7 <SCRIPTLANGUAGE="javascript"> 8 9 <!-- 10 11 /* 12 13 *该function执行copy指令14 15 */ 16 17 functionfn_doufucopy(){18 19 edit.select();20 21 document.execCommand('Copy');22 23 }24 25 /* 26 27 *该function执行paste指令28 29 */ 30 31 functionfn_doufupaste() {32 33 tt.focus();34 35 document.execCommand('paste');36 37 }38 39 /* 40 41 *该function用来创建一个超链接42 43 */ 44 45 functionfn_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 functionfn_change_backcolor()62 63 {64 65 document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以 66 67 }68 69 /* 70 71 *该function用来将选中的区块设为指定的前景色,改变选中区块的字体大小,改变字体,字体变粗变斜72 73 */ 74 75 functionfn_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 functionfn_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 functionfn_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 functionfn_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 functionfn_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 functionfn_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 <inputid="edit"value="范例"NAME="edit"><br> 326 327 <buttononclick="fn_doufucopy()"ID="Button1">Copy</button> <buttononclick="fn_doufupaste()"ID="Button2"> 328 329 paste</button><br> 330 331 <textareaid="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 <inputtype="button"value="创建CreateLink"onclick="fn_creatlink()"ID="Button3"NAME="Button3"><br> 350 351 <inputtype="button"value="改变文字背景色"onclick="fn_change_backcolor()"ID="Button4"NAME="Button4"><br> 352 353 <inputtype="button"value="改变文字前景色"onclick="fn_change_forecolor()"ID="Button5"NAME="Button5"><br> 354 355 <inputtype="button"value="给文字加线条"onclick="fn_change_selection()"ID="Button6"NAME="Button6"><br> 356 357 <inputtype="button"value="改变文字的排列"onclick="fn_format()"ID="Button7"NAME="Button7"><br> 358 359 <inputtype="button"value="删除或剪下选中的部分"onclick="fn_CutOrDel()"ID="Button8"NAME="Button8"><br> 360 361 <inputtype="button"value="插入Object"onclick="fn_InsObj()"ID="Button9"NAME="Button9"><br> 362 363 <inputtype="button"value="保存或打印文件"onclick="fn_save()"ID="Button10"NAME="Button10"><br> 364 365 <inputtype="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
JavaScript中的execCommand相关推荐
- Javascript中document.execCommand()的用法
document.execCommand()方法处理Html数据时常用语法格式如下: document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指 ...
- 如何用mshtml获得Javascript中function的返回值[mshtml]
marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/ ...
- 浅析 JavaScript 中的 函数 uncurrying 反柯里化
柯里化 柯里化又称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函数接受剩下的参数,这中间可嵌套多层这样的接受部分参数函数,直至返回最后结果. 因此柯里化的过程是 ...
- JavaScript中,this的绑定规则
对于 JavaScript 新手来说,this 是非常基础同时也难以理解的知识点. 比如下面的代码,this 指向就有三种方式. 在<你不知道的 JavaScript>一书中,我总算比较清 ...
- Javascript中undefined,NaN等特殊比较
以下内容转自: http://blog.csdn.net/hongweigg/article/details/38090093 1.问题:在Javascript中,typeof(undefined) ...
- Javascript中二进制数据处理方法
Javascript中二进制数据处理方法 转载于:https://www.cnblogs.com/motadou/archive/2012/02/19/2358514.html
- JavaScript 中的有限状态机
http://www.ibm.com/developerworks/cn/web/wa-finitemach/ JavaScript 中的有限状态机 Page navigation 系列文章 有限状态 ...
- 在Javascript中使用面向对象的编程
by Mike Koss March 26th, 2003 这是一篇,我个人认为最好的,Javascript面向对象编程的文章.翻译不好的地方,还望大家指正,谢谢. 如果您需要,可以访问下面的地址取得 ...
- 取出url中的字符_如何在JavaScript中解析URL:例如主机名,路径名,查询,哈希?...
统一资源定位符(缩写URL)是对Web资源(网页,图像,文件)的引用.URL指定资源位置和检索资源的机制(http,ftp,mailto). 例如,这是此博客文章的URL: 通常,您需要访问URL的特 ...
最新文章
- 报错:该字符串未被识别为有效的DateTime
- 类加载器双亲委派模式
- 如何将所有浏览器的div垂直居中?
- php orm url,PHP ORM使用之
- 基于 Java 2 运行时安全模型的线程协作--转
- linux c mysql 封装_本人对MYSQL C API做的一个封装,希望对linux C++程序员有点帮助,同时欢迎拍砖!...
- 如何分析线程转储–线程堆栈跟踪
- 存储过程中定义sql语句_学习SQL:用户定义的存储过程
- 使用R7版NDK搭建Android开发环境[不使用Cgywin]
- Visual Studio 2019 C# 断点调试 凯撒密码,单码密码实现
- ThinkpadT470接通电源开机显示电量0%充不进电且电源指示灯不亮的解决办法
- Linux内核学习编译流程
- 电子表格软件2013 免费版
- QT控件被其他控件遮盖
- win10亮度无法调节问题
- 【Linux】共享内存
- 1微秒等于多少皮秒_秒的换算:ms(毫秒),μs(微秒),ns(纳秒),ps(皮秒)
- emqx使用自制CA证书登录配置(双向认证)
- 首屏加载从11s到1s,详解前端性能优化
- LiteOS 消息队列