$('#copy').click(function () {
var copyText = '要复制的文本'
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard.writeText('copyText').then(() => {
alert("复制成功!")
}).catch((error) => {
alert("复制失败!请手动复制")
})
} else {
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed'
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em'
textArea.style.height = '2em'
textArea.style.padding = 0;
textArea.style.border = 'none'
textArea.style.outline = 'none'
textArea.style.boxShadow = 'none'
textArea.style.background = 'transparent'
textArea.value = copyText;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? '成功复制文本' : '复制文本失败'
alert(msg);
} catch (err) {
alert('发生错误');
}
document.body.removeChild(textArea);
}
});本文标签: 复制
版权声明:
本站转载作品版权归原作者及来源网站所有,原创内容作品版权归作者所有,任何内容转载、商业用途等均须联系原作者并注明来源。如果侵犯了您的权益请与我联系,我将在24小时内删除。