Copy to clipboard without Flash

后端 未结 8 923
情深已故
情深已故 2020-11-28 05:28

I found many solutions for copying to the clipboard, but they all either with flash, or for websites side. I\'m looking for method copy to clipboard automatically, without f

8条回答
  •  清歌不尽
    2020-11-28 06:25

    document.execCommand('copy') will do what you want. But there was no directly usable examples in this thread without cruft, so here it is:

    var textNode = document.querySelector('p').firstChild
    var range = document.createRange()
    var sel = window.getSelection()
    
    range.setStart(textNode, 0)
    range.setEndAfter(textNode)
    sel.removeAllRanges()
    sel.addRange(range)
    document.execCommand('copy')
    

提交回复
热议问题