复制功能 兼容ios

html:

复制
123456789

js:

function copyFn(event) {
  const range = document.createRange();
  range.selectNode(document.getElementById('copy'));
  const selection = window.getSelection();
  if(selection.rangeCount > 0) selection.removeAllRanges();
  selection.addRange(range);
  document.execCommand('copy');
  console.log("复制成功")
}
document.getElementById('box').addEventListener('click', copyFn, false);

你可能感兴趣的:(复制功能 兼容ios)