H5copy.js 493 B

123456789101112131415161718
  1. export default function h5Copy(content) {
  2. if (!document.queryCommandSupported('copy')) {
  3. // 不支持
  4. return false
  5. }
  6. let textarea = document.createElement("textarea")
  7. textarea.value = content
  8. textarea.readOnly = "readOnly"
  9. document.body.appendChild(textarea)
  10. textarea.select() // 选择对象
  11. textarea.setSelectionRange(0, content.length) //核心
  12. let result = document.execCommand("copy") // 执行浏览器复制命令
  13. textarea.remove()
  14. return result
  15. }