过时
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

When an HTML document has been switched to designMode , its document object exposes an execCommand method to run commands that manipulate the current editable region, such as form inputs or contentEditable 元素。

Most commands affect the document's selection (bold, italics, etc.), while others insert new elements (adding a link), or affect an entire line (indenting). When using contentEditable , execCommand() affects the currently active editable element.

句法

document.execCommand(aCommandName, aShowDefaultUI, aValueArgument)
					

返回值

A 布尔 也就是 false if the command is unsupported or disabled.

注意 : Only returns true if part of a user interaction. Don't try using the return value to verify browser support before calling a command.

参数

aCommandName
DOMString specifying the name of the command to execute. See 命令 for a list of possible commands.
aShowDefaultUI
布尔 indicating whether the default user interface should be shown. This is not implemented in Mozilla.
aValueArgument
For commands which require an input argument, is a DOMString providing that information. For example, insertImage requires the URL of the image to insert. Specify null if no argument is needed.

命令

backColor
Changes the document background color. In styleWithCss mode, it affects the background color of the containing block instead. This requires a <color> value string to be passed in as a value argument. Note that Internet Explorer uses this to set the text background color.
bold
Toggles bold on/off for the selection or at the insertion point. Internet Explorer uses the <strong> tag instead of <b> .
ClearAuthenticationCache

Clears all authentication credentials from the cache.

contentReadOnly

Makes the content document either read-only or editable. This requires a boolean true/false as the value argument. (Not supported by Internet Explorer.)

copy

Copies the current selection to the clipboard. Conditions of having this behavior enabled vary from one browser to another, and have evolved over time. Check the compatibility table to determine if you can use it in your case.

createLink
Creates an hyperlink from the selection, but only if there is a selection. Requires a URI string as a value argument for the hyperlink's href . The URI must contain at least a single character, which may be whitespace. (Internet Explorer will create a link with a null value.)
cut
Removes the current selection and copies it to the clipboard. When this behavior is enabled varies between browsers, and its conditions have evolved over time. Check the compatibility table 了解用法细节。
decreaseFontSize
添加 <small> tag around the selection or at the insertion point. (Not supported by Internet Explorer.)
defaultParagraphSeparator
Changes the paragraph separator used when new paragraphs are created in editable text regions. See Differences in markup generation 了解更多细节。
delete

删除当前选定。

enableAbsolutePositionEditor
Enables or disables the grabber that allows absolutely-positioned elements to be moved around. The grabber is disabled by default since Firefox 64 ( bug 1490641 ).
enableInlineTableEditing
Enables or disables the table row/column insertion and deletion controls. The controls are disabled by default since Firefox 64 ( bug 1490641 ).
enableObjectResizing
Enables or disables the resize handles on images, tables, and absolutely-positioned elements and other resizable objects. The handles are disabled by default since Firefox 64 ( bug 1490641 ).
fontName
Changes the font name for the selection or at the insertion point. This requires a font name string (like "Arial" ) as a value argument.
fontSize
Changes the font size for the selection or at the insertion point. This requires an integer from 1 - 7 as a value argument.
foreColor

Changes a font color for the selection or at the insertion point. This requires a hexadecimal color value string as a value argument.

formatBlock
Adds an HTML block-level element around the line containing the current selection, replacing the block element containing the line if one exists (in Firefox, <blockquote> is the exception — it will wrap any containing block element). Requires a tag-name string as a value argument. Virtually all block-level elements can be used. (Internet Explorer and Edge support only heading tags H1 H6 , ADDRESS ,和 PRE , which must be wrapped in angle brackets, such as "<H1>" )。
forwardDelete
Deletes the character ahead of the cursor 's position, identical to hitting the Delete key on a Windows keyboard.
heading
Adds a heading element around a selection or insertion point line. Requires the tag-name string as a value argument (i.e. "H1" , "H6" ). (Not supported by Internet Explorer and Safari.)
hiliteColor
Changes the background color for the selection or at the insertion point. Requires a color value string as a value argument. useCSS 必须为 true for this to function. (Not supported by Internet Explorer.)
increaseFontSize
添加 <big> tag around the selection or at the insertion point. (Not supported by Internet Explorer.)
indent

Indents the line containing the selection or insertion point. In Firefox, if the selection spans multiple lines at different levels of indentation, only the least indented lines in the selection will be indented.

insertBrOnReturn
Controls whether the Enter key inserts a <br> element, or splits the current block element into two. (Not supported by Internet Explorer.)
insertHorizontalRule
插入 <hr> element at the insertion point, or replaces the selection with it.
insertHTML

Inserts an HTML string at the insertion point (deletes selection). Requires a valid HTML string as a value argument. (Not supported by Internet Explorer.)

insertImage
Inserts an image at the insertion point (deletes selection). Requires a URL string for the image's src as a value argument. The requirements for this string are the same as createLink .
insertOrderedList
创建 numbered ordered list for the selection or at the insertion point.
insertUnorderedList
创建 bulleted unordered list for the selection or at the insertion point.
insertParagraph
插入 paragraph around the selection or the current line. (Internet Explorer inserts a paragraph at the insertion point and deletes the selection.)
insertText

Inserts the given plain text at the insertion point (deletes selection).

italic
Toggles italics on/off for the selection or at the insertion point. (Internet Explorer uses the <em> element instead of <i> )。
justifyCenter

Centers the selection or insertion point.

justifyFull

Justifies the selection or insertion point.

justifyLeft

Justifies the selection or insertion point to the left.

justifyRight

Right-justifies the selection or the insertion point.

outdent

Outdents the line containing the selection or insertion point.

paste

Pastes the clipboard contents at the insertion point (replaces current selection). Disabled for web content.

redo

Redoes the previous undo command.

removeFormat

Removes all formatting from the current selection.

selectAll

Selects all of the content of the editable region.

strikeThrough

Toggles strikethrough on/off for the selection or at the insertion point.

subscript
Toggles subscript on/off for the selection or at the insertion point.
superscript
Toggles superscript on/off for the selection or at the insertion point.
underline
Toggles underline on/off for the selection or at the insertion point.
undo

Undoes the last executed command.

unlink
移除 anchor element from a selected hyperlink.
useCSS

Toggles the use of HTML tags or CSS for the generated markup. Requires a boolean true/false as a value argument.

NOTE: This argument is logically backwards (i.e. use false to use CSS, true to use HTML) and unsupported by Internet Explorer. This has been deprecated in favor of styleWithCSS .
styleWithCSS
Replaces the useCSS 命令。 true modifies/generates style attributes in markup, false generates presentational elements.

范例

An example of how to use it on CodePen.

规范

规范 状态 注释
execCommand 非官方草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
execCommand 弃用 非标 Chrome 1 Edge 12 Firefox 1 IE 4 Opera 9 Safari 1.3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
ClearAuthenticationCache 命令 弃用 非标 Chrome No Edge 12 — 79 Firefox No IE 6 Opera No Safari No WebView Android No Chrome Android No Firefox Android No Opera Android No Safari iOS No Samsung Internet Android No
copy 命令 弃用 非标 Chrome 42 Edge 12 Firefox 41 IE 9 Opera 29 Safari 10 WebView Android 42 Chrome Android 42 Firefox Android 41 Opera Android 29 Safari iOS 10 Samsung Internet Android 4.0
cut 命令 弃用 非标 Chrome 42 Edge 12 Firefox 41 IE 9 Opera 29 Safari 10 WebView Android 42 Chrome Android 42 Firefox Android 41 Opera Android 29 Safari iOS 10 Samsung Internet Android 4.0
defaultParagraphSeparator 命令 弃用 非标 Chrome No Edge ≤18 — 79 Firefox 55 IE No Opera Yes Safari No WebView Android No Chrome Android No Firefox Android 55 Opera Android Yes Safari iOS No Samsung Internet Android No
insertBrOnReturn 命令 弃用 非标 Chrome No Edge No Firefox Yes IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android Yes Opera Android No Safari iOS No Samsung Internet Android No

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

另请参阅

元数据

  • 最后修改:
  1. DOM (文档对象模型)
  2. Document
  3. 构造函数
    1. Document()
  4. 特性
    1. alinkColor
    2. all
    3. anchors
    4. applets
    5. bgColor
    6. body
    7. characterSet
    8. childElementCount
    9. children
    10. compatMode
    11. contentType
    12. currentScript
    13. defaultView
    14. designMode
    15. dir
    16. doctype
    17. documentElement
    18. documentURI
    19. documentURIObject
    20. domain
    21. domConfig
    22. 嵌入
    23. fgColor
    24. firstElementChild
    25. forms
    26. fullscreen
    27. fullscreenEnabled
    28. head
    29. height
    30. hidden
    31. 图像
    32. 实现
    33. lastElementChild
    34. lastModified
    35. lastStyleSheetSet
    36. linkColor
    37. 链接
    38. location
    39. mozSyntheticDocument
    40. onabort
    41. onafterscriptexecute
    42. onanimationcancel
    43. onanimationend
    44. onanimationiteration
    45. onauxclick
    46. onbeforescriptexecute
    47. onblur
    48. oncancel
    49. oncanplay
    50. oncanplaythrough
    51. onchange
    52. onclick
    53. onclose
    54. oncontextmenu
    55. oncuechange
    56. ondblclick
    57. ondurationchange
    58. onended
    59. onerror
    60. onfocus
    61. onformdata
    62. onfullscreenchange
    63. onfullscreenerror
    64. ongotpointercapture
    65. oninput
    66. oninvalid
    67. onkeydown
    68. onkeypress
    69. onkeyup
    70. onload
    71. onloadeddata
    72. onloadedmetadata
    73. onloadend
    74. onloadstart
    75. onlostpointercapture
    76. onmousedown
    77. onmouseenter
    78. onmouseleave
    79. onmousemove
    80. onmouseout
    81. onmouseover
    82. onmouseup
    83. onoffline
    84. ononline
    85. onpause
    86. onplay
    87. onplaying
    88. onpointercancel
    89. onpointerdown
    90. onpointerenter
    91. onpointerleave
    92. onpointermove
    93. onpointerout
    94. onpointerover
    95. onpointerup
    96. onreset
    97. onresize
    98. onscroll
    99. onselect
    100. onselectionchange
    101. onselectstart
    102. onsubmit
    103. ontouchcancel
    104. ontouchstart
    105. ontransitioncancel
    106. ontransitionend
    107. onvisibilitychange
    108. onwheel
    109. origin
    110. plugins
    111. popupNode
    112. preferredStyleSheetSet
    113. readyState
    114. referrer
    115. rootElement
    116. 脚本
    117. scrollingElement
    118. selectedStyleSheetSet
    119. styleSheetSets
    120. timeline
    121. title
    122. tooltipNode
    123. URL
    124. visibilityState
    125. vlinkColor
    126. width
    127. xmlEncoding
    128. xmlVersion
  5. 方法
    1. adoptNode()
    2. append()
    3. caretRangeFromPoint()
    4. clear()
    5. close()
    6. createAttribute()
    7. createCDATASection()
    8. createComment()
    9. createDocumentFragment()
    10. createElement()
    11. createElementNS()
    12. createEntityReference()
    13. createEvent()
    14. createExpression()
    15. createExpression()
    16. createNodeIterator()
    17. createNSResolver()
    18. createNSResolver()
    19. createProcessingInstruction()
    20. createRange()
    21. createTextNode()
    22. createTouch()
    23. createTouchList()
    24. createTreeWalker()
    25. enableStyleSheetsForSet()
    26. evaluate()
    27. evaluate()
    28. execCommand()
    29. exitFullscreen()
    30. exitPointerLock()
    31. getAnimations()
    32. getBoxObjectFor()
    33. getElementById()
    34. getElementsByClassName()
    35. getElementsByName()
    36. getElementsByTagName()
    37. getElementsByTagNameNS()
    38. hasFocus()
    39. hasStorageAccess()
    40. importNode()
    41. mozSetImageElement()
    42. open()
    43. prepend()
    44. queryCommandEnabled()
    45. queryCommandSupported()
    46. querySelector()
    47. querySelector()
    48. querySelectorAll()
    49. querySelectorAll()
    50. registerElement()
    51. releaseCapture()
    52. replaceChildren()
    53. requestStorageAccess()
    54. write()
    55. writeln()
  6. 事件
    1. animationcancel
    2. animationend
    3. animationiteration
    4. animationstart
    5. copy
    6. cut
    7. DOMContentLoaded
    8. drag
    9. dragend
    10. dragenter
    11. dragexit
    12. dragleave
    13. dragover
    14. dragstart
    15. drop
    16. fullscreenchange
    17. fullscreenerror
    18. gotpointercapture
    19. keydown
    20. keypress
    21. keyup
    22. lostpointercapture
    23. paste
    24. pointercancel
    25. pointerdown
    26. pointerenter
    27. pointerleave
    28. pointerlockchange
    29. pointerlockerror
    30. pointermove
    31. pointerout
    32. pointerover
    33. pointerup
    34. readystatechange
    35. scroll
    36. selectionchange
    37. selectstart
    38. touchcancel
    39. touchend
    40. touchmove
    41. touchstart
    42. transitioncancel
    43. transitionend
    44. transitionrun
    45. transitionstart
    46. visibilitychange
    47. wheel
  7. 继承:
    1. 节点
    2. EventTarget
  8. DOM 相关页面
    1. AbortController
    2. AbortSignal
    3. AbstractRange
    4. Attr
    5. ByteString
    6. CDATASection
    7. CSSPrimitiveValue
    8. CSSValue
    9. CSSValueList
    10. CharacterData
    11. ChildNode
    12. 注释
    13. CustomEvent
    14. DOMConfiguration
    15. DOMError
    16. DOMErrorHandler
    17. DOMException
    18. DOMImplementation
    19. DOMImplementationList
    20. DOMImplementationRegistry
    21. DOMImplementationSource
    22. DOMLocator
    23. DOMObject
    24. DOMParser
    25. DOMPoint
    26. DOMPointInit
    27. DOMPointReadOnly
    28. DOMRect
    29. DOMString
    30. DOMTimeStamp
    31. DOMTokenList
    32. DOMUserData
    33. DocumentFragment
    34. DocumentType
    35. 元素
    36. ElementTraversal
    37. Entity
    38. EntityReference
    39. 事件
    40. EventTarget
    41. HTMLCollection
    42. MutationObserver
    43. 节点
    44. NodeFilter
    45. NodeIterator
    46. NodeList
    47. NonDocumentTypeChildNode
    48. ProcessingInstruction
    49. PromiseResolver
    50. 范围
    51. StaticRange
    52. 文本
    53. TextDecoder
    54. TextEncoder
    55. TimeRanges
    56. TreeWalker
    57. TypeInfo
    58. USVString
    59. UserDataHandler
    60. XMLDocument