使用Javascript将图像从浏览器复制到剪贴板? [英] Copy Image to Clipboard from Browser in Javascript?

查看:12
本文介绍了使用Javascript将图像从浏览器复制到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 javascript 中将图像复制到剪贴板?我知道怎么复制文字,但你能复制图片吗?

Is it possible to copy an image to the clipboard in javascript? I know how to copy text, but can you copy images?

这是安全限制吗?

推荐答案

最后一个答案是从 2010 年开始的,从那时起浏览器发生了很大变化.使用这个简单的功能,您可以将您想要的任何内容(文本、图像、表格等)(在您的页面上)复制到剪贴板.该函数接收元素 id 或元素本身.

The last answer is from 2010 and browsers have changed a lot since then. With this simple function, you can copy whatever you want (text, images, tables, etc) (on your page) to the clipboard. The function receive the element id or the element itself.

function copyElementToClipboard(element) {
  window.getSelection().removeAllRanges();
  let range = document.createRange();
  range.selectNode(typeof element === 'string' ? document.getElementById(elementName) : element);
  window.getSelection().addRange(range);
  document.execCommand('copy');
  window.getSelection().removeAllRanges();
 }

这篇关于使用Javascript将图像从浏览器复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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