使用 JavaScript 将内容复制到剪贴板或从剪贴板粘贴内容

发布于 2024-10-03 09:00:43 字数 144 浏览 9 评论 0原文

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

I need javascript that copies the content into clipboard and user should be able to access the content from clipboard. i.e There are two buttons copy and paste. When you click the copy button the text from text area say textarea content is copied to clipboard and when you click the paste button it should be pasted in some other textarea or if possible in an editor (TinyMCE).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绝對不後悔。 2024-10-10 09:00:43

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

我曾经使用 Flash 组件 Clippy 执行过复制到剪贴板功能,它非常易于使用。
你可以在 Github 上查看它的存储库,不过它只复制到剪贴板。

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

There isn't a good/well-tested solution using javascript, most solutions only works in IE

I did the copy-to-clipboard functionality once, using a Flash component, Clippy, it's very easy to use.
You can take a look at its repository on Github, it does only the copy to clipboard though..

Another good library to manage the clipboard, also in Flash, is ZeroClipboard

两相知 2024-10-10 09:00:43

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

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>

You can use This code.If someone copy content from your website.

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>

画骨成沙 2024-10-10 09:00:43

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

<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>

A simple google would return me this code... :-)

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文