使用 JavaScript/jquery 将图像复制到剪贴板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8743559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:26:59  来源:igfitidea点击:

Copying an image to clipboard using JavaScript/jquery

javascriptjqueryimageclipboard

提问by kranthi

I need to copy an image to clipboard using JavaScript/jquery and I am using the following js to achieve that.

我需要使用 JavaScript/jquery 将图像复制到剪贴板,我正在使用以下 js 来实现这一点。

function copyImageToClipBoard() {
            var div = document.getElementById('chart1');
            div.contentEditable = true;
            var controlRange;
            if (document.body.createControlRange) {
                controlRange = document.body.createControlRange();
                controlRange.addElement(div);
                controlRange.execCommand('Copy');                   
            }
            div.contentEditable = false;
        }

It works fine locally in IE. But when I tried to test it from other machines IE, to paste the image into MS word I need to use the Paste Special-> Device Independent Bitmap option, otherwise I cannot see the image pasted.

它在 IE 本地工作正常。但是当我尝试从其他机器 IE 测试它时,要将图像粘贴到 MS word 中,我需要使用“选择性粘贴”->“设备无关位图”选项,否则我看不到粘贴的图像。

I was wondering if it has anything to do with the environment of the m/c. If so is there any other option which works anywhere?

我想知道是否与m/c的环境有关。如果是这样,还有其他选择可以在任何地方使用吗?

回答by rburk

Clipboard access on the web is not desirable because of the security implications.

由于安全隐患,在网络上访问剪贴板是不可取的。