JS复制文本到剪切板

2022-10-22,,,

// 是否支持复制
export const issupportcopy = ((!!document.querycommandsupported) && document.querycommandsupported('copy'));
const copytempelement = document.createelement('textarea');
copytempelement.setattribute('style', 'margin:0;padding:0;width:0;height:0;position:absolute;');
document.body.appendchild(copytempelement);
// 复制文本剪切板
export function copytext(text) {
  let succeeded;
  try {
    copytempelement.value = text;
    copytempelement.select();
    succeeded = document.execcommand('copy');
  } catch (err) {
    succeeded = false;
  }
  return succeeded;
}

《JS复制文本到剪切板.doc》

下载本文的Word格式文档,以方便收藏与打印。