点击按钮,复制文本

$('body').on('tap', '.copy-btn', function (e) {
  e.preventDefault();
  e.stopPropagation();
  const range = document.createRange();
  range.selectNode($(this).prev('.text')[0]);
  const selection = window.getSelection();
  if (selection.rangeCount > 0) {
    selection.removeAllRanges();
  }
  selection.addRange(range);
  document.execCommand('copy');
  alert('文本已复制!');
});

posted @ 2020-11-16 15:02  ZerlinM  阅读(100)  评论(0编辑  收藏  举报