文章分类:Web前端
execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令。处理Html数据时常用如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的"2D-Position"),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的"false"即表示不显示对话框),动态参数一般为一可用值或属性值(如下例中的"true")。
普通的方式是激活一个<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)
=======分割线========
在相应的document下执行execCommand(),可以默认对该document内选择的内容进行操作,如果没有默认的选择内容,则不会有变化,对于上面的iframe,如果要对iframe内部的内容进行操作,要首先取的改frame内部的window,然后是该document,对document进行操作
其中contentWindow是iframe内部一个属性,是获取指定的 frame 或 iframe 的 window 对象
下面是从晚上获取的iframe的一些属性:
- 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 清除当前选中区的选中状态。
- <HTML>
- <HEAD>
- <TITLE>JavaScript--execCommand指令集</TITLE>
- <SCRIPT LANGUAGE="javascript">
- <!--
- function fn_doufucopy(){
- edit.select();
- document.execCommand('Copy');
- }
- function fn_doufupaste() {
- tt.focus();
- document.execCommand('paste');
- }
- function fn_creatlink()
- {
- document.execCommand('CreateLink',true,'true');//弹出一个对话框输入URL
- //document.execCommand('CreateLink',false,'http://www.51js.com');
- }
- function fn_change_backcolor()
- {
- document.execCommand('BackColor',true,'#FFbbDD');//true或false都可以
- }
- function fn_change_forecolor()
- {
- //指定前景色
- document.execCommand('ForeColor',false,'#BBDDCC');//true或false都可以
- //指定背景色
- document.execCommand('FontSize',false,7); //true或false都可以
- //字体必须是系统支持的字体
- document.execCommand('FontName',false,'标楷体'); //true或false都可以
- //字体变粗
- document.execCommand('Bold');
- //变斜体
- document.execCommand('Italic');
- }
- function fn_change_selection()
- {
- //将选中的文字加下划线
- document.execCommand('Underline');
- //在选中的文字上划粗线
- document.execCommand('StrikeThrough');
- //将选中的部分文字变细
- document.execCommand('SuperScript');
- //将选中区块的下划线取消掉
- document.execCommand('Underline');
- }
- function fn_format()
- {
- //有序列排列
- document.execCommand('InsertOrderedList');
- //实心无序列排列
- document.execCommand('InsertUnorderedList');
- //空心无序列排列
- document.execCommand('Indent');
- }
- function fn_CutOrDel()
- {
- //删除选中的区块
- //document.execCommand('Delete');
- //剪下选中的区块
- document.execCommand('Cut');
- }
- function fn_InsObj()
- {
- //重设为一个fieldset
- //插入一个水平线
- //document.execCommand('InsertHorizontalRule',true,"aa");
- //插入一个iframe
- //document.execCommand('InsertIFrame',true,"aa");
- //插入一个InsertImage,设为true时需要图片,false时不需图片
- //document.execCommand('InsertImage',false,"aa");
- //插入一个checkbox
- //document.execCommand('InsertInputCheckbox',true,"aa");
- //插入一个file类型的object
- //document.execCommand('InsertInputFileUpload',false,"aa");
- //插入一个hidden
- //插入一个InputImage
- //插入一个Password
- //document.execCommand('InsertInputPassword',true,"aa");
- //插入一个Radio
- //document.execCommand('InsertInputRadio',false,"aa");
- //插入一个Reset
- //document.execCommand('InsertInputReset',true,"aa");
- //插入一个Submit
- //document.execCommand('InsertInputSubmit',false,"aa");
- //插入一个input text
- //document.execCommand('InsertInputText',false,"aa");
- //插入一个textarea
- //document.execCommand('InsertTextArea',true,"aa");
- //插入一个 select list box
- //document.execCommand('InsertSelectListbox',false,"aa");
- //插入一个single select
- document.execCommand('InsertSelectDropdown',true,"aa");
- //插入一个line break(硬回车??)
- //document.execCommand('InsertParagraph');
- //插入一个marquee
- //用于取消选中的阴影部分
- //document.execCommand('Unselect');
- //选中页面上的所有元素
- //document.execCommand('SelectAll');
- }
- function fn_save()
- {
- //第二个参数为欲保存的文件名
- document.execCommand('SaveAs','mycodes.txt');
- //打印整个页面
- //document.execCommand('print');
- }
- -->
- </SCRIPT>
- </HEAD>
- <body>
- <input id="edit" value="范例" NAME="edit"><br>
- <button onclick="fn_doufucopy()" ID="Button1">Copy</button> <button onclick="fn_doufupaste()" ID="Button2">
- paste</button><br>
- <textarea id="tt" rows="10" cols="50" NAME="tt"></textarea>
- <hr>
- <br>
- 浮沉聚散变化又再,但是总可卷土重来.<br>
- 天若有情天亦老,人间正道是沧桑.<br>
- 都怪我,太执着,却也等不到花开叶落.<br>
- <br>
- Please select above letters, then click following buttons:<br>
- <hr>
- <input type="button" value="创建CreateLink" onclick="fn_creatlink()" ID="Button3" NAME="Button3"><br>
- <input type="button" value="改变文字背景色" onclick="fn_change_backcolor()" ID="Button4" NAME="Button4"><br>
- <input type="button" value="改变文字前景色" onclick="fn_change_forecolor()" ID="Button5" NAME="Button5"><br>
- <input type="button" value="给文字加线条" onclick="fn_change_selection()" ID="Button6" NAME="Button6"><br>
- <input type="button" value="改变文字的排列" onclick="fn_format()" ID="Button7" NAME="Button7"><br>
- <input type="button" value="删除或剪下选中的部分" onclick="fn_CutOrDel()" ID="Button8" NAME="Button8"><br>
- <input type="button" value="插入Object" onclick="fn_InsObj()" ID="Button9" NAME="Button9"><br>
- <input type="button" value="保存或打印文件" onclick="fn_save()" ID="Button10" NAME="Button10"><br>
- <input type="button" value="测试Refresh属性" onclick="document.execCommand('Refresh')" ID="Button11"
- NAME="Button11">
- </body>
- </HTML>
普通的方式是激活一个<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)
=======分割线========
在相应的document下执行execCommand(),可以默认对该document内选择的内容进行操作,如果没有默认的选择内容,则不会有变化,对于上面的iframe,如果要对iframe内部的内容进行操作,要首先取的改frame内部的window,然后是该document,对document进行操作
- var editor = document.getElementById(iframid);
- var editorDoc = editor.contentWindow.document();
- editorDoc.execCommand("","","");
其中contentWindow是iframe内部一个属性,是获取指定的 frame 或 iframe 的 window 对象
下面是从晚上获取的iframe的一些属性:
- ALIGN align 设置或获取表格排列。
- ALLOWTRANSPARENCY allowTransparency 设置或获取对象是否可为透明。
- APPLICATION APPLICATION 表明对象的内容是否为 HTML 应用程序(HTA),以便免除浏览器安全模式。
- ATOMICSelectION 指定元素及其内容是否可以一不可见单位统一选择。
- ACCELERATOR accelerator 设置或获取表明对象是否包含快捷键的字符串。
- BEGIN begin 设置或获取时间线在该元素上播放前的延迟时间。
- BORDER border 设置或获取框架间的空间,包括 3D 边框。
- background-attachment backgroundAttachment 设置或获取背景图像如何附加到文档内的对象中。
- background-color backgroundColor 设置或获取对象内容后的颜色。
- background-position-x backgroundPositionX 设置或获取 backgroundPosition 属性的 x 坐标。
- background-position-y backgroundPositionY 设置或获取 backgroundPosition 属性的 y 坐标。
- behavior behavior 设置或获取 DHTML 行为的位置。
- border-bottom borderBottom 设置或获取对象下边框的属性。
- border-bottom-color borderBottomColor 设置或获取对象下边框的颜色。
- border-bottom-style borderBottomStyle 设置或获取对象下边框的样式。
- border-bottom-width borderBottomWidth 设置或获取对象下边框的宽度。
- border-color borderColor 设置或获取对象的边框颜色。
- border-left borderLeft 设置或获取对象左边框的属性。
- border-left-color borderLeftColor 设置或获取对象左边框的颜色。
- border-left-style borderLeftStyle 设置或获取对象左边框的样式。
- border-left-width borderLeftWidth 设置或获取对象左边框的宽度。
- border-right borderRight 设置或获取对象右边框的属性。
- border-right-color borderRightColor 设置或获取对象右边框的颜色。
- border-right-style borderRightStyle 设置或获取对象右边框的样式。
- border-right-width borderRightWidth 设置或获取对象右边框的宽度。
- border-style borderStyle 设置或获取对象上下左右边框的样式。
- border-top borderTop 设置或获取对象上边框的属性。
- border-top-color borderTopColor 设置或获取对象上边框的颜色。
- border-top-style borderTopStyle 设置或获取对象上边框的样式。
- border-top-width borderTopWidth 设置或获取对象上边框的宽度。
- border-width borderWidth 设置或获取对象上下左右边框的宽度。
- bottom bottom 设置或获取对象相对于文档层次中下个定位对象的底部的位置。
- canHaveChildren 获取表明对象是否可以包含子对象的值。
- canHaveHTML 获取表明对象是否可以包含丰富的 HTML 标签的值。
- CLASS className 设置或获取对象的类。
- contentWindow 获取指定的 frame 或 iframe 的 window 对象。
- clear clear 设置或获取对象是否允许在其左侧、右侧或两边放置浮动对象,以防下段文本显示在浮动对象上。
- clip clip 设置或获取定位对象的哪个部分可见。
- cursor cursor 设置或获取当鼠标指针指向对象时所使用的鼠标指针。
- display display 设置或获取对象是否要渲染。
- DATAFLD dataFld 设置或获取由 dataSrc 属性指定的绑定到指定对象的给定数据源的字段。
- DATASRC dataSrc 设置或获取用于数据绑定的数据源。
- disabled 获取表明用户是否可与该对象交互的值。
- END end 设置或获取表明元素结束时间的值,或者元素设置为重复的简单持续终止时间。
- firstChild 获取对象的 childNodes 集合的第一个子对象的引用。
- FRAMEBORDER frameBorder 设置或获取是否显示框架的边框。
- float styleFloat 设置或获取文本要绕排到对象的哪一侧。
- filter filter 设置或获取应用于对象的滤镜或滤镜集合。
- hasMedia 获取一个表明元素是否为 HTML+TIME 媒体元素的 Boolean 值。
- HEIGHT height 设置或获取对象的高度。
- HIDEFOCUS hideFocus 设置或获取表明对象是否显式标明焦点的值。
- HSPACE hspace 设置或获取对象的水平边距。
- height height 设置或获取对象的高度。
- ID id 获取标识对象的字符串。
- innerText 设置或获取位于对象起始和结束标签内的文本。
- isContentEditable 获取表明用户是否可编辑对象内容的值。
- isDisabled 获取表明用户是否可与该对象交互的值。
- isMultiLine 获取表明对象的内容是包含一行还是多行的值。
- isTextEdit 获取是否可使用该对象创建一个 TextRange 对象。
- LANG lang 设置或获取要使用的语言。
- LANGUAGE language 设置或获取当前脚本编写用的语言。
- layout-grid layoutGrid 设置或获取指定文本字符版面的组合文档格线属性。
- layout-grid-mode layoutGridMode 设置或获取文本布局网格是否使用二维。
- left left 设置或获取对象相对于文档层次中下个定位对象的左边界的位置。
- lastChild 获取该对象 childNodes 集合中最后一个子对象的引用。
- longDesc 设置或获取对象长描述的统一资源标识符(URI)。
- MARGINHEIGHT marginHeight 设置或获取显示框架中文本之前的上下边距高度。
- MARGINWIDTH marginWidth 设置或获取显示框架中文本之前的左右边距宽度。
- margin margin 设置或获取对象的上下左右边距。
- margin-bottom marginBottom 设置或获取对象的下边距宽度。
- margin-left marginLeft 设置或获取对象的左边距宽度。
- margin-right marginRight 设置或获取对象的右边距宽度。
- margin-top marginTop 设置或获取对象的上边距宽度。
- NAME name 设置或获取框架的名称。
- nextSibling 获取对此对象的下一个兄弟对象的引用。
- nodeName 获取特定结点类型的名称。
- nodeType 获取所需结点的类型。
- nodeValue 设置或获取结点的值。
- offsetHeight 获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的高度。
- offsetLeft 获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置。
- offsetParent 获取定义对象 offsetTop 和 offsetLeft 属性的容器对象的引用。
- offsetTop 获取对象相对于版面或由 offsetTop 属性指定的父坐标的计算顶端位置。
- offsetWidth 获取对象相对于版面或由父坐标 offsetParent 属性指定的父坐标的宽度。
- onOffBehavior 获取表明指定的 Microsoft DirectAnimation 行为是否正在运行的对象。
- outerHTML 设置或获取对象及其内容的 HTML 形式。
- outerText 设置或获取对象的文本。
- ownerDocument 设置或获取结点关联的 document 对象。
- overflow-x overflowX 设置或获取当内容超出对象宽度时如何管理对象内容。
- overflow-y overflowY 设置或获取当内容超出对象高度时如何管理对象内容。
- parentElement 获取对象层次中的父对象。
- parentNode 获取文档层次中的父对象。
- parentTextEdit 获取文档层次中可用于创建包含原始对象的 TextRange 的容器对象。
- previousSibling 获取对此对象的上一个兄弟对象的引用。
- pixelBottom 设置或获取对象的下方位置。
- pixelHeight 设置或获取对象的高度。
- pixelLeft 设置或获取对象的左侧位置。
- pixelRight 设置或获取对象的右侧位置。
- pixelTop 设置或获取对象的上方位置。
- pixelWidth 设置或获取对象的宽度。
- posBottom 设置或获取以 bottom 标签属性指定的单位的对象下方位置。
- posHeight 设置或获取以 height 标签属性指定的单位的对象高度。
- position position 设置或获取对象所使用的定位方式。
- posLeft 设置或获取以 left 标签属性指定的单位的对象左侧位置。
- posRight 设置或获取以 right 标签属性指定的单位的对象右侧位置。
- posTop 设置或获取以 top 标签属性指定的单位的对象上方位置。
- posWidth 设置或获取以 width 标签属性指定的单位的对象宽度。
- readyState 获取对象的当前状态。
- readyState 获取表明对象当前状态的值。
- recordNumber 获取数据集中生成对象的原始记录。
- right right 设置或获取对象相对于文档层次中下个已定位的对象的右边界的位置。
- scopeName 获取为该元素定义的命名空间。
- SCROLLING scrolling 设置或获取框架是否可被滚动。
- SECURITY SECURITY 获取表明 frame 或 iframe 的源文件是否应用了特定的安全限制的值。
- sourceIndex 获取对象在源序中的依次位置,即对象出现在 document 的 all 集合中的顺序。
- SRC src 设置或获取要由对象装入的 URL。
- STYLE 为该设置元素设置内嵌样式。
- SYNCMASTER syncMaster 设置或获取时间容器是否必须在此元素上同步回放。
- SYSTEMBITRATE 获取系统中大约可用带宽的 bps。
- SYSTEMCAPTION 表明是否要显示文本来代替演示的的音频部分。
- SYSTEMLANGUAGE 表明是否在用户计算机上的选项设置中选中了给定语言。
- SYSTEMOVERDUBORSUBTITLE 指定针对那些正在观看演示但对被播放的音频所使用的语言并不熟悉的用户来说是否要渲染配音或字幕。
- TABINDEX tabIndex 设置或获取定义对象的 Tab 顺序的索引。
- tagName 获取对象的标签名称。
- tagUrn 设置或获取在命名空间声明中指定的统一资源名称(URN)。
- TIMECONTAINER timeContainer 设置或获取与元素关联的时间线类型。
- TITLE title 设置或获取对象的咨询信息(工具提示)。
- text-autospace textAutospace 设置或获取自动留空和文本的窄空间宽度调整。
- top top 设置或获取对象相对于文档层次中下个定位对象的上边界的位置。
- uniqueID 获取为对象自动生成的唯一标识符。
- UNSelectABLE 指定该元素不可被选中。
- VSPACE vspace 设置或获取对象的垂直边距。
- visibility visibility 设置或获取对象的内容是否显示。
- WIDTH width 设置或获取对象的宽度。
- z-index zIndex 设置或获取定位对象的堆叠次序。
- zoom zoom 设置或获取对象的放大比例。