如何使用JavaScript复制到剪贴板? - How do I copy to the clipboard in JavaScript?

问题:

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

I have tried: 我试过了: spa

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

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


解决方案:

参考一: https://stackoom.com/question/1g72/如何使用JavaScript复制到剪贴板
参考二: https://oldbug.net/q/1g72/How-do-I-copy-to-the-clipboard-in-JavaScript
相关文章
相关标签/搜索