当前位置:首页 > Web开发 > 正文

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

2024-03-31 Web开发

 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实现:把网页中的文字复制到剪切板

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/42504.html