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: '复制失败'})
  }
}                       

 

posted @ 2021-02-21 14:33  leejay-blog  阅读(3406)  评论(0编辑  收藏  举报