function copyText(txt: string) { return new Promise((resolve, reject) => { const domCopy = function () { const input = document.createElement('input'); input.value = txt; input.style.position = "absolute"; input.style.top = "-100px" document.body.appendChild(input) setTimeout(() => { input.select() let r = document.execCommand("copy"); if (r) { resolve("复制成功") } else { reject("复制失败") } input.remove(); }, 100) } if (navigator.clipboard) { navigator.clipboard.writeText(txt).then(resolve).catch(() => { domCopy() }); } else { domCopy() } }) }
标签:document,浏览器,JS,兼容,resolve,复制,input,txt,domCopy From: https://www.cnblogs.com/zzg1022/p/18581828