| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/API/Document/execCommand | [Back] [Original] |
Get to know MDN better
Avoid using this feature in new projects.
The execCommand() method is "not implemented consistently or fully by user agents, and it is not expected that this will change in the foreseeable future."
This feature may be a candidate for removal from web standards or browsers.
Consider using the following features instead: Async clipboardcontenteditable.
:
:
execCommand() DOM execCommand() document.queryCommandSupported()
execCommand contenteditable
execCommand() API
:
execCommand() beforeinput input execCommand() execCommand() Firefox 82 execCommand() 1634262
execCommand(commandName, showDefaultUI, valueArgument)
commandNamebackColor styleWithCss <color>
boldcontentReadOnlytrue false
copycreateLink href URI URI 1
cutdecreaseFontSizedefaultParagraphSeparatordeleteenableAbsolutePositionEditorFirefox 64 (Firefox 1490641)
enableInlineTableEditing/ Firefox 64 (Firefox 1490641)
enableObjectResizingFirefox 64 (Firefox 1490641)
fontName"Arial"
fontSize 1 - 7
foreColor16
formatBlock HTML (Firefox <blockquote> ) Edge H1H6, ADDRESS, PRE "<H1>"
forwardDeleteheading "H1" "H6" (Safari )
hiliteColor color useCSS true
increaseFontSizeindentFirefox
insertBrOnReturnEnter <br>
insertHorizontalRuleinsertHTML HTML TrustedHTML ()
HTML
: HTML DOM API (XSS)
insertImage () src URL createLink
insertLineBreakinsertOrderedListinsertUnorderedListinsertParagraphinsertText()
italicjustifyCenterjustifyFulljustifyLeftjustifyRightoutdentpasteAPI UI
redoundo
removeFormatselectAllstrikeThroughsubscriptsuperscriptunderlineundounlinkuseCSS HTML CSS true/false
:
false CSS true HTML styleWithCSS
styleWithCSSuseCSS true style false
AutoUrlDetectshowDefaultUIMozilla
valueArgument insertImage URL null
false
:
document.execCommand() true
2 HTML <textarea> <pre> contenteditable
"Bold" "Italic" insertText
<h2>textarea</h2>
<div class="actions" data-for="textarea">
<button data-el="b">Bold</button>
<button data-el="i">Italic</button>
</div>
<textarea class="editarea"></textarea>
<h2>contenteditable</h2>
<div class="actions" data-for="pre">
<button data-el="b">Bold</button>
<button data-el="i">Italic</button>
</div>
<pre contenteditable="true" class="editarea"></pre>
//
const buttonContainers = document.querySelectorAll(".actions");
for (const buttonContainer of buttonContainers) {
const buttons = buttonContainer.querySelectorAll("button");
const pasteTarget = buttonContainer.getAttribute("data-for");
for (const button of buttons) {
const elementName = button.getAttribute("data-el");
button.addEventListener("click", () =>
insertText(`<${elementName}></${elementName}>`, pasteTarget),
);
}
}
//
function insertText(newText, selector) {
const textarea = document.querySelector(selector);
textarea.focus();
let pasted = true;
try {
if (!document.execCommand("insertText", false, newText)) {
pasted = false;
}
} catch (e) {
console.error("error caught:", e);
pasted = false;
}
if (!pasted) {
console.error("execCommand ");
}
}
<button id="paste"></button>
<hr />
<textarea id="text_box"></textarea>
const pasteButton = document.querySelector("#paste");
const textBox = document.querySelector("#text_box");
pasteButton.addEventListener("click", () => {
textBox.focus();
let pasted = document.execCommand("paste", false);
if (!pasted) {
textBox.textContent =
"execCommand ";
}
});
API UI
DocumentactiveElementactiveViewTransitionadoptedStyleSheetsalinkColorallanchorsappletsbgColorbodycharacterSetchildElementCountchildrencompatModecontentTypecookiecurrentScriptcustomElementRegistrydefaultViewdesignModedirdoctypedocumentElementdocumentURIdomainembedsfeaturePolicyfgColorfirstElementChildfontsformsfragmentDirectivefullscreenfullscreenElementfullscreenEnabledheadhiddenimagesimplementationlastElementChildlastModifiedlastStyleSheetSetlinkColorlinkslocationpictureInPictureElementpictureInPictureEnabledpluginspointerLockElementpreferredStyleSheetSetprerenderingreadyStatereferrerrootElementscriptsscrollingElementselectedStyleSheetSetstyleSheetsstyleSheetSetstimelinetitleURLvisibilityStatevlinkColorxmlEncodingxmlVersionadoptNode()append()ariaNotify()browsingTopics()caretPositionFromPoint()caretRangeFromPoint()clear()close()createAttribute()createAttributeNS()createCDATASection()createComment()createDocumentFragment()createElement()createElementNS()createEvent()createExpression()createNodeIterator()createNSResolver()createProcessingInstruction()createRange()createTextNode()createTouch()createTouchList()createTreeWalker()elementFromPoint()elementsFromPoint()enableStyleSheetsForSet()evaluate()execCommand()exitFullscreen()exitPictureInPicture()exitPointerLock()getAnimations()getElementById()getElementsByClassName()getElementsByName()getElementsByTagName()getElementsByTagNameNS()getSelection()hasFocus()hasPrivateToken()hasRedemptionRecord()hasStorageAccess()hasUnpartitionedCookieAccess()importNode()moveBefore()mozSetImageElement()open()prepend()queryCommandEnabled()queryCommandState()queryCommandSupported()querySelector()querySelectorAll()releaseCapture()replaceChildren()requestStorageAccess()requestStorageAccessFor()startViewTransition()write()writeln()AbortControllerAbortSignalAbstractRangeAttrCDATASectionCharacterDataCommentCustomEventDOMErrorDOMExceptionDOMImplementationDOMParserDOMTokenListDocumentFragmentDocumentTypeElementEventEventTargetHTMLCollectionMutationObserverMutationRecordNamedNodeMapNodeNodeIteratorNodeListProcessingInstructionQuotaExceededErrorRangeShadowRootStaticRangeTextTreeWalkerXMLDocumentXPathEvaluatorXPathExpressionXPathResultXSLTProcessor| Web Proxy Viewer | New URL | Original Page |