无法使document.execCommand与复制文本一起正常工作 [英] cannot get document.execCommand to work properly with copying text

查看:260
本文介绍了无法使document.execCommand与复制文本一起正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

function createTextArea() {
    var t = document.createElement("textArea");
    textArea = document.body.appendChild(t);

    return textArea;
}

function copy(str) {
    var textArea = createTextArea();
    textArea.value = str;
    textArea.select();
    document.execCommand("Copy");
}

copy("hello")

现在,当我尝试粘贴时,放置在文本区域中的文本没有出现。有人知道我在做什么错吗?

Now, when I try to paste, the text I placed into the textarea is not appearing. Does anyone know what I'm doing wrong?

推荐答案

根据文档,您有错字。来自在Mozilla中进行富文本编辑有关执行命令

You have a typo according to documentatin. From Rich-Text Editing in Mozilla about executing commands

复制
将当前选择复制到剪贴板。必须在 user.js 首选项文件中启用剪贴板功能

copy Copies the current selection to the clipboard. Clipboard capability must be enabled in the user.js preference file.

并查看这两篇文章:
如何配置Firefox以允许Javascript拦截从剪贴板粘贴的值?
使用JavaScript复制到所有浏览器的剪贴板

And see those two posts: How do you configure Firefox to allow Javascript to intercept a value pasted from the clipboard? and Copy to Clipboard for all Browsers using javascript

因此,是否可以这样做似乎很成问题。

So, it seems very questionable if you can do that.

这篇关于无法使document.execCommand与复制文本一起正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆