我想单击按钮将<div id="content">
中的文本复制到剪贴板。有没有办法做到这一点与JavaScript或jQuery,但没有使用插件。我不需要它是跨浏览器的,只是在Firefox上。将文本复制到剪贴板,仅限Firefox,无插件
$('#copy').click(function(){
var cont = $('#content').text();
//how to copy cont to clipboar?
});
我想单击按钮将<div id="content">
中的文本复制到剪贴板。有没有办法做到这一点与JavaScript或jQuery,但没有使用插件。我不需要它是跨浏览器的,只是在Firefox上。将文本复制到剪贴板,仅限Firefox,无插件
$('#copy').click(function(){
var cont = $('#content').text();
//how to copy cont to clipboar?
});
您将需要使用Flash。阅读以下答案:
使用Zero Clipboard。这是最好的。
不,HTML5之前没有办法。但是实现这一点甚至非常棘手。所有的插件使用闪存复制到剪贴板。您可以使用zClip http://www.steamdev.com/zclip/。
什么gion_13所说的也需要闪光,因为你可以注意到在文章中的链接。所以使用微小的插件复制到剪贴板是没有害处的:)
工作到2012年11月左右,Mozilla通过更新销毁它。现在我已经有了一个解决方法:打开带有内容的新窗口进行复制。
感谢马修·富兰琛的DataURL想法(https://stackoverflow.com/a/3665147/1120146)
/**
* 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;
}
的
可能重复[如何复制在JavaScript到剪贴板?](http://stackoverflow.com/questions/400212/how-to -copy到剪贴板功能于JavaScript的) – peirix