使用javascript将内容复制/粘贴到剪贴板中

时间:2010-11-22 11:13:37

标签: javascript clipboard copy-paste

我需要将内容复制到剪贴板的javascript,用户应该能够从剪贴板访问内容。即复制和粘贴有两个按钮。当您单击复制按钮时,文本区域中的文本会将textarea内容复制到剪贴板,当您单击粘贴按钮时,应将其粘贴到其他文本区域中,或者如果可能,则粘贴到编辑器(TinyMCE)中。

3 个答案:

答案 0 :(得分:2)

使用javascript没有一个好/经过良好测试的解决方案,大多数解决方案仅适用于IE

我使用Flash组件Clippy完成了一次复制到剪贴板的功能,它非常易于使用。 您可以在Github上查看其存储库,但它只复制到剪贴板..

管理剪贴板的另一个好库(也在Flash中)是ZeroClipboard

答案 1 :(得分:0)

您可以使用此代码。如果有人从您的网站复制内容。

document.addEventListener('copy', (event) => {
  const pagelink = `\n\nRead more at: ${document.location.href}`;
  let copyText = document.getSelection();
  copyText = 'use website for read content';
  event.clipboardData.setData('text', copyText + pagelink);
  event.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</h1>

答案 2 :(得分:-1)

一个简单的谷歌会给我这个代码......: - )

<SCRIPT language="JavaScript">
<!--
function highlightmetasearch() {
    document.post.message.select(); document.post.message.focus();
}
function copymetasearch() {
    highlightmetasearch();
    textRange = document.post.message.createTextRange();
    textRange.execCommand("RemoveFormat");
    textRange.execCommand("Copy");
    alert("This post has been copied to your clipboard.\nIf this post is lost when you submit it you can easily repost it.\nAlways use this feature before posting!");
}
    // -->
</SCRIPT>
相关问题