Click button copy to clipboard using jQuery

前端 未结 21 2077
醉话见心
醉话见心 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
    2020-11-22 00:02

    Even better approach without flash or any other requirements is clipboard.js. All you need to do is add data-clipboard-target="#toCopyElement" on any button, initialize it new Clipboard('.btn'); and it will copy the content of DOM with id toCopyElement to clipboard. This is a snippet that copy the text provided in your question via a link.

    One limitation though is that it does not work on safari, but it works on all other browser including mobile browsers as it does not use flash

    $(function(){
      new Clipboard('.copy-text');
    });
    
    
    
    

    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s

    copy Text

提交回复
热议问题