clipboard.js implements the function of copying browser content to the system clipboard in pure JavaScript (no Flash). Can be used in browser and Node environments. Support Chrome 42, Firefox 41, IE 9, Opera 29
Official website: https://github.com/zenorocha/clipboard.js
Software download: http://www.jb51.net/jiaoben/385604.html
Official usage: https://zenorocha.github.io/clipboard.js/#example-text
Sample code:
Official example one:
Online demo: http://demo.jb51.net/js/2015/clipboard/
<script src="clipboard.min.js"></script> <textarea id="bar" cols="62" rows="5" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">脚本之家是国内专业的网站建设资源、脚本编程学习类网站,提供asp、php、asp.net、javascript、jquery、vbscript、dos批处理、网页制作、网络编程、网站建设等编程资料。</textarea> <!-- Trigger --> <button class="btn" data-clipboard-action="copy" data-clipboard-target="#bar"> Cut to clipboard </button> <script> var clipboard = new Clipboard('.btn'); clipboard.on('success', function(e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); alert("复制成功"); e.clearSelection(); }); clipboard.on('error', function(e) { console.error('Action:', e.action); console.error('Trigger:', e.trigger); }); </script>
Official example two:
<script src="dist/clipboard.min.js"></script> <button class="btn"> <script type="text/javascript"> var clipboard = new Clipboard('.btn'); clipboard.on('success', function(e) { console.info('Action:', e.action); console.info('Text:', e.text); console.info('Trigger:', e.trigger); e.clearSelection(); }); clipboard.on('error', function(e) { console.error('Action:', e.action); console.error('Trigger:', e.trigger); }); </script>