使用JavaScript将所选文本复制并粘贴到剪贴板 [英] Copy and paste the selected text to the clipboard using JavaScript

查看:91
本文介绍了使用JavaScript将所选文本复制并粘贴到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为系统构建自定义的右键单击菜单,我需要知道如何制作JavaScript函数来复制所选文本,基本上就像原始的右键单击菜单一样100%复制。

I'm building a custom right-click menu for my system and I need to know how can I make a JavaScript function to copy the selected text, basically 100% like the original right-click menu does.

我知道Flash的解决方法。我想用JavaScript做到这一点。

I'm aware of the Flash work-arounds. I want to do this in JavaScript.

到目前为止,我看到的每个答案都只是一个半答案,因为它们都没有解释如何为该按钮制作复制按钮。选定的文本-他们所做的只是复制预定义的文本或文本框中的文本。

Every answer I've seen so far is only a half-answer because none of them explains how to make a copy button for the selected text - all what they do is copy a pre-defined text or a text from a textbox.

推荐答案

对于非IE浏览器,您很可能必须使用Flash解决方案。但是,对于IE,此方法非常有效:

For non-IE browsers you will most likely have to use a flash solution. For IE, however, this method works perfectly:

function copyToClipboard(s) {           //only works in IE :(
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('text', s);
    }
}

这篇关于使用JavaScript将所选文本复制并粘贴到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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