在 JavaScript 中复制和粘贴剪贴板。或 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3475293/
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-08-23 04:48:08  来源:igfitidea点击:

Copy and paste clipboard in JavaScript. or jQuery

javascriptjqueryclipboard

提问by rahul

How do I copy content thats present in a divtag and paste it to applications like Paint, Powerpoint etc.

如何复制div标签中存在的内容并将其粘贴到 Paint、Powerpoint 等应用程序中。

回答by Sebastian J.

It is not easy but possible:

这并不容易,但可能:

function copyToClipboard(meintext) {  
if (window.clipboardData)   
     window.clipboardData.setData("Text", meintext);  
else if (window.netscape) {  
     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');  
     var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);  
     if (!clip)  
          return false;  
     var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);  
     if (!trans)  
          return false;  
     trans.addDataFlavor('text/unicode');  
     var str = new Object();  
     var len = new Object();  
     var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);  
     str.data=meintext;  
     trans.setTransferData("text/unicode",str,meintext.length*2);  
     var clipid=Components.interfaces.nsIClipboard;  
     if (!clipid)  
          return false;  
     clip.setData(trans,null,clipid.kGlobalClipboard);  
}  
     return false;  
}

Please note. The first two lines are for IE.

请注意。前两行是针对 IE 的。

All the following for Firefox. And for Firefox the clipboard has to be enabled:

以下所有适用于 Firefox。对于 Firefox,必须启用剪贴板:

Open about:configset signed.applets.codebase_principal_supportto true.

打开about:config设置signed.applets.codebase_principal_supporttrue

Or just use some Flash-stuff :)

或者只是使用一些 Flash 的东西:)

回答by Nealv

you can use jquery clipboard, check the homepage here

您可以使用jquery 剪贴板,请在此处查看主页