如何使用JavaScript复制到剪贴板?

瘦欲@ 提交于 2020-03-25 06:16:44

3 月,跳不动了?>>>

问题:

What is the best way to copy text to the clipboard? 将文本复制到剪贴板的最佳方法是什么? (multi-browser) (多浏览器)

I have tried: 我试过了:

function copyToClipboard(text) {
    if (window.clipboardData) { // Internet Explorer
        window.clipboardData.setData("Text", text);
    } else {  
        unsafeWindow.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
        const clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);  
        clipboardHelper.copyString(text);
    }
}

but in Internet Explorer it gives a syntax error. 但是在Internet Explorer中,它会给出语法错误。 In Firefox, it says unsafeWindow is not defined . 在Firefox中,它说unsafeWindow is not defined

A nice trick without flash: How does Trello access the user's clipboard? 一个没有闪光灯的好技巧: Trello如何访问用户的剪贴板?


解决方案:

参考一: https://stackoom.com/question/1g72/如何使用JavaScript复制到剪贴板
参考二: https://oldbug.net/q/1g72/How-do-I-copy-to-the-clipboard-in-JavaScript
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!