Click button copy to clipboard using jQuery

前端 未结 21 2076
醉话见心
醉话见心 2020-11-21 23:11

How do I copy the text inside a div to the clipboard? I have a div and need to add a link which will add the text to the clipboard. Is there a solution for this?

         


        
21条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 23:51

    As of 2020, you should use the Clipboard Api.

    navigator.clipboard.writeText('text here you want to copy').then(function () {
        alert('It worked! Do a CTRL - V to paste')
    }, function () {
        alert('Failure to copy. Check permissions for clipboard')
    });
    

    Here is more info about interacting with the clipboard

提交回复
热议问题