H5/纯JS实现:把网页中的文字复制到剪切板


 copy =() => {
    const dom = document.getElementById(`collect-text-${t.Id}`)
    const selection = window.getSelection()
    const range = document.createRange()

    if (!dom || !selection || !range) return

    // 选择复制目标
    range.selectNodeContents(dom)
    selection.removeAllRanges()
    selection.addRange(range)

    // 已复制文字
    console.log('selectedText', selection.toString())

    // 执行复制
    document.execCommand('copy')

    // 去除所有选中
    selection.removeAllRanges()
   // 复制内容成功,通过ctrl+v粘贴使用!
  }

 

参考资料:

select :  https://github.com/zenorocha/select

 

clipboard.js :  https://github.com/zenorocha/clipboard.js

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM