简体   繁体   English

Javascript:document.execCommand跨浏览器?

[英]Javascript: document.execCommand cross-browser?

I just stumble on a piece of code which I never saw before: 我偶然发现了一段我以前从未见过的代码:

document.execCommand('Copy');

which seems to copy the clipboard contents to the element in focus. 这似乎将剪贴板内容复制到焦点元素。

Is this functionality available cross-browser? 跨浏览器是否可以使用此功能?


I found a page that shows a compatibility matrix for document.execCommand . 我找到了一个显示document.execCommand兼容性矩阵的页面

This is for 'design mode' where the browser effectively turns the document into an editor. 这适用于“设计模式”,浏览器有效地将文档转换为编辑器。 The execCommand API originated with IE and was later added to HTML5. execCommand API源自IE,后来被添加到HTML5中。 Exactly which commands are supported, as well as their behavior varies across browsers. 确切支持哪些命令,以及它们的行为因浏览器而异。 Clipboard access is considered a security risk. 剪贴板访问被视为安全风险。

Yes, I have used it in IE, Chrome, Safari. 是的,我在IE,Chrome,Safari中使用过它。 If it works for these browser then it should work for the rest. 如果它适用于这些浏览器,那么它应该适用于其余的。 Anyway, the execCommand method of the document object is used to execute commands relating to the built in Rich Text Editing features in the browser. 无论如何, document对象的execCommand方法用于执行与浏览器中内置的富文本编辑功能相关的命令。 The syntax of the execCommand is as follow: document.execCommand(command, uiBool, argument) execCommand的语法如下: document.execCommand(command, uiBool, argument)

The command parameter is the command to execute - bold, underline, font, etc. command参数是要执行的命令 - 粗体,下划线,字体等。

Then you have the uiBool which is the boolean value that specifies whether or not the default user interface should be shown. 然后你有了uiBool这是一个布尔值,它指定是否应该显示默认用户界面。

And the last parameter is the argument use for some commands that requires that we pass an argument. 最后一个参数是一些命令的参数用法,它要求我们传递一个参数。 If no argument is required by the command we pass a value of null as the third parameter. 如果命令不需要参数,我们将值null作为第三个参数传递。

Example: 例:

document.getElementById("whateverID").document.execCommand('bold', false, null);

or: 要么:

document.getElementById("whateverID").document.execCommand('bold', false, <a variable nae>);

Update: Well, document.execCommand is documented in the Mozilla DOM documentation , but its description there looks slightly different from the MSDN documentation. 更新:嗯, document.execCommand记录在Mozilla DOM文档中 ,但其描述与MSDN文档略有不同。

I'm still pretty sure it's not in the ECMA-262 standard. 我仍然非常确定它不符合ECMA-262标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM