JS: copyToClipboard

2019-11-02  本文已影响0人  柳正来

The following function is a handy util function that you can use to copy a string into clipboard.

function copyToClipboard(str) {
  const el = document.createElement('textarea');
  el.value = str;
  el.setAttribute('readonly', '');
  el.style.position = 'absolute';
  el.style.left = '-9999px';
  document.body.appendChild(el);
  el.select();
  document.execCommand('copy');
  document.body.removeChild(el);
};

Reference

上一篇 下一篇

猜你喜欢

热点阅读