js实现复制dom节点内容功能

实现复制dom节点中的内容:

function copyText (ele) {
  try {
    let selection = window.getSelection()
    if (selection.rangeCount > 0) selection.removeAllRanges()
    let range = document.createRange()
    range.selectNodeContents(ele)
    selection.addRange(range)
    document.execCommand("copy")
    selection.removeAllRanges()
    return Promise.resolve({success: true, message: '复制成功'})
  } catch (error) {
    return Promise.resolve({success: false, message: '复制失败'})
  }
}                       
原文地址:https://www.cnblogs.com/leejay6567/p/14425723.html