JavaScript中的简单复制粘贴功能 [英] Simple copy paste function in JavaScript

查看:71
本文介绍了JavaScript中的简单复制粘贴功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript中对文本进行简单的复制和粘贴?我想实现这一点,当我在 textarea 中选择一些文本时,可以单击一个按钮进行复制,然后可以转到另一个页面,右键单击另一个 textarea 并选择粘贴。

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.

推荐答案

尝试一下:

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.

我希望它能对您有所帮助。

I hope it helps you.

这篇关于JavaScript中的简单复制粘贴功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆