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: '復制失敗'})
  }
}                       

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM