HTML DOM中的DOM execCommand()方法用于执行用户在可编辑所选部分上指定的命令。
用法:
document.execCommand( command, showUI, value )
参数:此方法接受下面列出的三个参数:
command:此参数保存在选定部分执行的命令的名称。 HTML中有许多命令,其中一些是:背景色,粗体,复制,剪切,删除等。
showUI:它包含指示是否显示UI的布尔值。
value:此参数保存命令的值。
返回值:它返回一个布尔值。如果支持该命令,则返回True,否则返回False。
例:
HTML DOM execCommand() Method
document.designMode = "on";
function execfunction(event) {
document.execCommand("bold");
}
Welcome to GeekforGeeks
Document execCommand() Method
The execCommand() method executes a specified
command on selected text or section.
Select some text in this page, and move the mouse
pointer away from it. It will make the text bold.
输出:
选择内容之前:
选择内容后:
移动鼠标指针后:
支持的浏览器:下面列出了DOM execCommand()方法支持的浏览器:
chrome
Internet Explorer 9.0
Opera 29.0
Firefox 41.0
苹果Safari 20.0