我在我的项目中使用了以下代码。。它对我有用。。
<a rel="tooltip" data-placement="top" title="Copy code" class="copytext-btn copyText" href="javascript:void(0);"><i class="code-file-icn"></i></a>
单击事件:
jQuery(".copyText").click(function(e)
{
e.preventDefault();
copyTextToClipboard(jQuery('.GeneratedText').text());
});
function copyTextToClipboard(text)
{
var textArea = document.createElement("textarea");
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
if(successful)
{
}
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
}
catch (err)
{
console.log('Oops, unable to copy');
}
}