Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Midas 所见即所得编辑功能

简介

Midas是一个Gecko内建的富文本编辑器的代号。Midas可以通过JS激活。当Midas可用时,文档将变成用户可编辑状态。Midas的脚本是基于IE所支持的DHTML commands。IE中通过设置文档对象的designMode属性让整个文档变成可编辑状态;这也是Gecko中的Midas采用的调用方式。IE也支持让某个特定元素进入可编辑状态,只要在钙元素上使用contentEditable属性;从FireFox3开始,Gecko也支持使用contentEditable。只要调用了Midas,就可以使用document文档对象的一些方法相关方法。

属性

document.designMode
将该属性设置为“on”,该文档将可编辑。

注意

整个文档变为可编辑状态后,开发者常将此文档载入iframe框架,然后在父文档中编写代码。根据标准,iframe元素拥有contentDocument属性引用其内联框架的document对象,同样还有contentWindow属性可以引用内联框架的window对象。

除了内建的命令意外,通过操纵Selectionrange对象可以实现更高级的编辑功能。编辑文档的时候用到这些对象可以提高编辑器的友好性。

案例

该示例代码展示了最基本的代码结构:

<html>
	<head>
		<title>Simple Edit Box</title>
	</head>
	<body>
		<iframe
			id="MidasForm"
			src="about:blank"
			onload="this.contentDocument.designMode='on';"
		></iframe>
	</body>
</html>

方法

document.execCommand
执行给定的命令。
document.queryCommandEnabled
文档在当前状态下是否可以执行给定的命令。
document.queryCommandIndeterm
检测当前选定的内容是否处于不确定的状态。
document.queryCommandState
检测给出的命令是否已在当前选择的内容上执行。
document.queryCommandValue
检测给定命令操作的当前文档、范围或选定内容的当前值。

支持的命令

Command Value Description
backcolor

颜色代码

设置文档背景色。

bold  

如果没有选中文本,将在光标处插入一个标志位。

如果选中了一段文本并且该文本内容已经是粗体,那么这些粗体样式会被移除。否则,所有文本都会从普通状态变为粗体。

contentReadOnly  

该命令可以让编辑器变为只读状态(true)或可编辑状态(false)。当页面需要进行其他活动(比如保存编辑状态,利用AJAX向服务器提交数据)的时候,该命令会很有用。

copy  

如果选择了一段文本,该命令可以将其内容拷贝到剪贴板。如果没有选中文本,什么也不会发生。

note: this command won't work without setting a pref or using signed JS. See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-C) with or without the signed JS or any code on the page to handle it.

createlink A URI. This command will not do anything if no selection is made. If there is a selection, a link will be inserted around the selection with the url parameter as the href of the link.
cut   If there is a selection, this command will copy the selection to the clipboard and remove the selection from the edit control. If there isn't a selection, nothing will happen.

note: this command won't work without setting a pref or using signed JS. See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-X) with or without the signed JS or any code on the page to handle it.

decreasefontsize   This command will add a <small> tag around selection or at insertion point.
delete   This command will delete all text and objects that are selected. If no text is selected it deletes one character to the right. This is similar to the Delete button on the keyboard.
fontname A font name This command will set the font face for a selection or at the insertion point if there is no selection.

The given string is such as would be used in the "face" attribute of the font tag, i.e., a comma-separated list of font names.

fontsize A number This command will set the fontsize for a selection or at the insertion point if there is no selection.

The given number is such as would be used in the "size" attribute of the font tag.

forecolor A color code This command will set the text color of the selection or at the insertion point.
formatblock H1, H2, H3, H4, H5, H6, P, DIV, ADDRESS, BLOCKQUOTE (more?) The selection surrounded by the given block element.
heading H1, H2, H3, H4, H5, H6 Selected block will be formatted as the given type of heading.
hilitecolor A color code This command will set the hilite color of the selection or at the insertion point. It only works with styleWithCSS enabled.
increasefontsize   This command will add a <big> tag around selection or at insertion point.
indent   Indent the block where the caret is located. If the caret is inside a list, that item becomes a sub-item one level deeper.
insertbronreturn true/false Selects whether pressing return inside a paragraph creates another paragraph or just inserts a <br> tag.
inserthorizontalrule null/string (when string is the Line's id) This command will insert a horizontal rule (line) at the insertion point.

Does it delete the selection? Yes!

inserthtml A string. This command will insert the given html into the <body> in place of the current selection or at the caret location.

The given string is the HTML to insert.

insertimage A URI. This command will insert an image (referenced by the given url) at the insertion point.
insertorderedlist   Depends on the selection. If the caret is not inside a non-LI block, that block becomes the first LI and an OL. If the caret is inside a bulleted item, the bulleted item becomes a numbered item.
insertunorderedlist   Depends on the selection. If the caret is not inside a non-LI block, that block becomes the first LI and UL. If the caret is inside a numbered item, the numbered item becomes a bulleted item.
insertparagraph   Inserts a new paragraph.
italic   If there is no selection, the insertion point will set italic for subsequently typed characters.

If there is a selection and all of the characters are already italic, the italic will be removed. Otherwise, all selected characters will become italic.

justifycenter   Center-aligns the current block.
justifyfull   Fully-justifies the current block.
justifyleft   Left-aligns the current block.
justifyright   Right aligns the current block.
outdent   Outdent the block where the caret is located. If the block is not indented prior to calling outdent, nothing will happen.

If the caret is in a list item, the item will bump up a level in the list or break out of the list entirely.

paste   This command will paste the contents of the clipboard at the location of the caret. If there is a selection, it will be deleted prior to the insertion of the clipboard's contents.

note: this command won't work without setting a pref or using signed JS. user_pref("capability.policy.policynames", "allowclipboard"); user_pref("capability.policy.allowclipboard.Clipboard.paste", "allAccess"); See: more about security preferences

note: the shortcut key will automatically trigger this command (typically accel-V) with or without the signed JS or any code on the page to handle it.

redo   This command will redo the previous undo action. If undo was not the most recent action, this command will have no effect.

note: the shortcut key will automatically trigger this command (typically accel-shift-Z)

removeformat   Removes inline formatting from the current selection.
selectall   This command will select all of the contents within the editable area.

note: the shortcut key will automatically trigger this command (typically accel-A)

strikethrough   If there is no selection, the insertion point will set strikethrough for subsequently typed characters.

If there is a selection and all of the characters are already striked, the strikethrough will be removed. Otherwise, all selected characters will have a line drawn through them.

styleWithCSS   This command is used for toggling the format of generated content. By default (at least today), this is true. An example of the differences is that the "bold" command will generate <b> if the styleWithCSS command is false and generate css style attribute if the styleWithCSS command is true.
subscript   If there is no selection, the insertion point will set subscript for subsequently typed characters.

If there is a selection and all of the characters are already subscripted, the subscript will be removed. Otherwise, all selected characters will be drawn slightly lower than normal text.

superscript   If there is no selection, the insertion point will set superscript for subsequently typed characters.

If there is a selection and all of the characters are already superscripted, the superscript will be removed. Otherwise, all selected characters will be drawn slightly higher than normal text.

underline   If there is no selection, the insertion point will set underline for subsequently typed characters.

If there is a selection and all of the characters are already underlined, the underline will be removed. Otherwise, all selected characters will become underlined.

undo   This command will undo the previous action. If no action has occurred in the document, then this command will have no effect.

note: the shortcut key will automatically trigger this command (typically accel-Z)

unlink   If the insertion point is within a link or if the current selection contains a link, the link will be removed and the text will remain.

不推荐

readonly   This command has been replaced with contentReadOnly. It takes the same values as contentReadOnly, but the meaning of true and false are inversed.
useCSS   This command has been replaced with styleWithCSS. It takes the same values as styleWithCSS, but the meaning of true and false are inversed.

 

文档标签和贡献者

 此页面的贡献者: ZhangJianxiang, ziyunfei
 最后编辑者: ZhangJianxiang,