Javascript 将文本复制到剪贴板,仅firefox,无插件

Javascript 将文本复制到剪贴板,仅firefox,无插件,javascript,jquery,clipboard,Javascript,Jquery,Clipboard,我想单击一个按钮,将中的文本复制到剪贴板中。有没有一种方法可以在不使用插件的情况下使用javascript或jquery实现这一点。我不需要跨浏览器,只需要在Firefox上 $('#copy').click(function(){ var cont = $('#content').text(); //how to copy cont to clipboar? }); 你需要用Flash来做这个。阅读以下答案: 使用。这是最好的。不,在HTML5之前没有办法。但实现这一点甚至很棘手

我想单击一个按钮,将
中的文本复制到剪贴板中。有没有一种方法可以在不使用插件的情况下使用javascript或jquery实现这一点。我不需要跨浏览器,只需要在Firefox上

$('#copy').click(function(){
   var cont = $('#content').text();
   //how to copy cont to clipboar?
});

你需要用Flash来做这个。阅读以下答案:


使用。这是最好的。

不,在HTML5之前没有办法。但实现这一点甚至很棘手。所有插件都使用flash复制到剪贴板。你可以使用zClip


gion_13所说的也需要flash,因为你可以在文章中注意到链接已经被删除。因此,使用一个小插件复制到剪贴板并没有什么害处:)

一直工作到2012年11月左右,然后Mozilla通过更新将其销毁。现在我有了一个解决办法:打开一个新窗口,里面有要复制的内容

感谢Matthew Flaschen的DataURL想法()

可能重复的
/**
*   To use the clipboard from Mozilla / NS / Firefox:
*
*   Clipboard access works only up to Firefox 14 :-( (thanks to those security fanatics)
*
*   Solution for later versions: Window pops up with text inside (data url)
*   
*   In "about:config" : 
*       set signed.applets.codebase_principal_support = true!
*
*   @param text: The text which shold be copied to clipboard
*   @param fallbackContentType: The content type of the text, if clipboard access 
*                               doesn't work, i.e. "text/csv"
*                               default: text/plain
*/

function toClipboard(text, fallbackContentType) {
    var success = false;
    if (window.clipboardData)    {
           // the IE-manier
        window.clipboardData.setData("Text", text);
        success = true;
    }
    else if (window.netscape)  {

        if(netscape.security.PrivilegeManager != undefined) {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

            var clip = Components.classes['@mozilla.org/widget/clipboard;1'].getService(Components.interfaces.nsIClipboard);
            var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);

            if(typeof(clip) == "object" && typeof(trans) == "object") {
                trans.addDataFlavor('text/unicode');

                var stingSupporter = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);

                stingSupporter.data = text;
                trans.setTransferData("text/unicode", stingSupporter, text.length * 2);
                var clipid = Components.interfaces.nsIClipboard;
                clip.setData(trans, null, clipid.kGlobalClipboard);

                success = true;
            }
        }
        else { // Firefox > v15
            // Create Data URL
            if(fallbackContentType == undefined) fallbackContentType = "text/plain";

            var url = "data:"+ fallbackContentType +"," + encodeURIComponent(text);
            window.open(url);
        }
    }
    return success;
}