JS 将字符串复制到剪贴板
2021-04-05 04:26
标签:false app val end child toc document style move JS 将字符串复制到剪贴板 标签:false app val end child toc document style move 原文地址:https://www.cnblogs.com/wkk2020/p/12515135.html const el = document.createElement(‘textarea‘);
el.value = str;
el.setAttribute(‘readonly‘, ‘‘);
el.style.position = ‘absolute‘;
el.style.left = ‘-9999px‘;
document.body.appendChild(el);
const selected =
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
document.execCommand(‘copy‘);
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
};
// 事例
copyToClipboard(‘Lorem ipsum‘);
// ‘Lorem ipsum‘ copied to clipboard