How I can make simple copy and paste for text in JavaScript? I would like to achieve that when I select some text in a textarea
, then I can click on a button to copy it, then I can go to another page right click in another textarea
and choose paste.
Take a look at this library: https://github.com/zeroclipboard/zeroclipboard
You cannot access the clipboard in JavaScript, meaning flash is more or less your only option.
Use this
function Copy() {
if(window.clipboardData) {
window.clipboardData.clearData();
window.clipboardData.setData("Text", document.getElementById('txtacpy').value);
}
}
function paste() {
if(window.clipboardData) {
document.getElementById('txtapaste').value = window.clipboardData.getData("Text");
}
}
<a href="javascript:Copy();">Copy</a>
<br />
<input type="text" name="txtacpy" id ="txtacpy"/>
<br />
<a href="javascript:paste();">Paste</a>
<br />
<input type="text" name="txtapaste" id="txtapaste"/>
Its a simple copy and paste function. Its working well with IE.
I hope its help to you
Assuming you want to fetch user keyboard actions, you probably want to use Hotkeys: https://github.com/jeresig/jquery.hotkeys
I think easiest way (and working in all browsers) is to watch keys pressed by user and if he press CTRL+C, save some data you want to copy into some variable.
I mean something like this:
var myClipboardVariable;
document.onkeyup = function(e){
if ((e.key == 'c') && e.ctrlKey){
// save data (you want to copy) into variable
myClipboardVariable = ....//some data
}
if ((e.key == 'v') && e.ctrlKey){
// paste saved data
.... paste your data from variable myClipboardVariable
}
}
来源:https://stackoverflow.com/questions/5579232/simple-copy-paste-function-in-javascript