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

Posted 有蚊子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了H5/纯JS实现:把网页中的文字复制到剪切板相关的知识,希望对你有一定的参考价值。

   const dom = document.getElementById(`span1`)
    const selection = window.getSelection()
    const range = document.createRange()

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

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

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

    // 去除所有选中,否则该元素会显示为 被选中的颜色
    selection.removeAllRanges()
   

 

以上是关于H5/纯JS实现:把网页中的文字复制到剪切板的主要内容,如果未能解决你的问题,请参考以下文章