Simple copy paste function in JavaScript

大兔子大兔子 提交于 2019-12-01 05:27:14
Peeter

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.

Anand Thangappan

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
        }

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