将文本复制到剪贴板的最佳方法是什么?(多浏览器)
我试过了:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但在Internet Explorer中,它会出现语法错误.在Firefox中,它说unsafeWindow is not defined.
没有闪存的好技巧:Trello如何访问用户的剪贴板?
我试图在单击按钮时复制URL。一些我曾尝试过但无法正常工作的方法。 http://www.w3schools.com/code/tryit.asp?filename=FAF25LWITXR5
function Copy()
{
var Url = document.createElement("textarea");
Url.innerHTML = window.location.href;
Copied = Url.createTextRange();
Copied.execCommand("Copy");
}Run Code Online (Sandbox Code Playgroud)
<div>
<input type="button" value="Copy Url" onclick="Copy();" />
<br />
Paste: <textarea rows="1" cols="30"></textarea>
</div>Run Code Online (Sandbox Code Playgroud)