美文网首页
复制到剪贴板

复制到剪贴板

作者: friendshi洛初Udo邭 | 来源:发表于2021-05-05 10:17 被阅读0次

// 复制到剪贴板
export const copyText = (value) => {
return new Promise((resolve, reject) => {
try {
if (window.clipboardData) {
window.clipboardData.setData("Text", ${value || ''});
if (window.clipboardData.getData('text/plain') == ${value || ''} || window.clipboardData.getData('Text') == ${value || ''}) {
resolve(true);
}else{
reject(false)
}
} else {
let copyInput = document.getElementsByClassName("copyInput")[0];
if (!copyInput) {
copyInput = document.createElement('textarea');
copyInput.value = ${value || ''};
document.body.appendChild(copyInput);
copyInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
copyInput.className = 'copyInput';
copyInput.style.display = 'none';
} else {
copyInput.remove();
let newCopyInput = document.createElement('textarea');
newCopyInput.value = ${value || ''};
document.body.appendChild(newCopyInput);
newCopyInput.select(); // 选择对象
document.execCommand("Copy"); // 执行浏览器复制命令
newCopyInput.className = 'copyInput';
newCopyInput.style.display = 'none';
}
resolve(true);
}
} catch (err) {
reject(err)
}
})

}

相关文章

网友评论

      本文标题:复制到剪贴板

      本文链接:https://www.haomeiwen.com/subject/xhekdltx.html