js复制文本到粘贴板

2023-01-18

export const copyToClipboard = (text, callback) => {
  var tag = document.createElement('textarea');
  tag.setAttribute('id', 'cp_input');
  tag.value = text;
  document.getElementsByTagName('body')[0].appendChild(tag);
  document.getElementById('cp_input').select();
  document.execCommand('copy');
  document.getElementById('cp_input').remove();
  callback && callback(text);
}