简体   繁体   English

简单复制粘贴function到JavaScript

[英]Simple copy paste function in JavaScript

How I can make simple copy and paste for text in JavaScript?如何对 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.我想实现的是,当我 select 文本区域中的一些textarea时,我可以单击一个按钮将其复制,然后我可以 go 到另一个页面右键单击另一个textarea并选择粘贴。

Take a look at this library: https://github.com/zeroclipboard/zeroclipboard看看这个库: https : //github.com/zeroclipboard/zeroclipboard

You cannot access the clipboard in JavaScript, meaning flash is more or less your only option.您无法在 JavaScript 中访问剪贴板,这意味着 Flash 或多或少是您唯一的选择。

Try this:尝试这个:

 function copy() { if(window.clipboardData) { window.clipboardData.clearData(); window.clipboardData.setData("Text", document.getElementById('txToCopy').value); } } function paste() { if(window.clipboardData) { document.getElementById('txToPaste').value = window.clipboardData.getData("Text"); } }
 <a href="javascript:copy();">Copy</a> <br /> <input type="text" name="txToCopy" id ="txToCopy"/> <br /><br /> <a href="javascript:paste();">Paste</a> <br /> <input type="text" name="txToPaste" id="txToPaste"/>

It's a simple copy and paste function.这是一个简单的复制和粘贴功能。 Its working well in IE.它在 IE 中运行良好。

I hope it helps you.我希望它能帮助你。

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.我认为最简单的方法(并在所有浏览器中工作)是观察用户按下的键,如果他按下 CTRL+C,保存一些你想复制到某个变量中的数据。

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 : //github.com/jeresig/jquery.hotkeys

have a look at this MDN article .看看这篇 MDN 文章

If you just want to copy user selected text you can do:如果您只想复制用户选择的文本,您可以:

document.execCommand("copy");

if you need to select it previously:如果您需要之前选择它:

document.getElementById('txToPaste').select();
  • In my case this code didn't work - it turns out select() don't work for disabled inputs.在我的情况下,此代码不起作用 - 结果select()不适用于disabled输入。

  • you don't need any special permissions if you run it from an onClick event listener, if you want another event to trigger the copy you are a bit in trubbles.如果您从onClick事件侦听器运行它,则不需要任何特殊权限,如果您希望另一个事件触发副本,您就有点麻烦了。

  • it works for me on Firefox and chrome, MDN says it won't work for safari but I haven't tested it.它在 Firefox 和 chrome 上对我有用,MDN 说它对 safari 不起作用,但我还没有测试过。

You can use ClipBoard API for this,您可以为此使用剪贴板 API,

//copy
navigator.clipboard.writeText("yuuhhhh");
//paste ,this needs user permission 
navigator.clipboard.readText()

Now you can use.then function to do what ever you want.现在你可以使用.then function 做任何你想做的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM