https://mp.weixin.qq.com/s/yrQ1mOe69wHScuUssh9Yfw <!DOCTYPE html> <html lang="zh-ch"> <meta chaset="utf-8"> <head> <title>JS实现复制到剪贴板(支持IE和Firefox)</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function copy_clip(txt) { if (window.clipboardData) { window.clipboardData.clearData(); window.clipboardData.setData("Text", txt); } else if (navigator.userAgent.indexOf("Opera") != -1) { window.location = txt; } else if (window.netscape) { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); } catch (e) { alert("您的firefox安全限制限制您进行剪贴板操作,请在新窗口的地址栏里输入'about:config'然后找到'signed.applets.codebase_principal_support'设置为true'"); return false; } var clip = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard); if (!clip) return; var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable); if (!trans) return; 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); var copytext = txt; str.data = copytext; trans.setTransferData("text/unicode", str, copytext.length * 2); var clipid = Components.interfaces.nsIClipboard; if (!clip) return false; clip.setData(trans, null, clipid.kGlobalClipboard); } } </script> </head> <body> <input type="text" id="txt" value="http://www.gzui.net/" /> <button onclick="copy_clip(document.getElementById('txt').value);alert('恭喜,复制成功!');">复制内容</button> </body> </html> 提示:你可以先修改部分代码再运行。 转载请注明:有爱前端 » JS实现复制到剪贴板(支持IE和Firefox) 喜欢 (0)or分享 (0)