问题
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