美文网首页
复制功能 兼容ios

复制功能 兼容ios

作者: mindy1031 | 来源:发表于2021-01-12 17:13 被阅读0次

html:

<div id="box">复制</div>
<div id="copy">123456789</div>

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

      本文链接:https://www.haomeiwen.com/subject/urjeaktx.html