cannot get document.execCommand to work properly with copying text

痞子三分冷 提交于 2019-12-25 01:32:17

问题


Here is my code:

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?


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/20171088/cannot-get-document-execcommand-to-work-properly-with-copying-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!