var clipboard = new Clipboard('.btn');
</script>
演示实例:
<script src="http://demo.jb51.net/js/2015/clipboard/clipboard.min.js"></script> <textarea id="bar" cols="62" rows="5" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">这些是即将被复制的示例文字。</textarea> <button class="btn" data-clipboard-action="copy" data-clipboard-target="#bar">点击复制</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>
clipboard.js的用法很灵活,可以实现多种复制或剪切效果。具体可参阅官方使用教程:https://clipboardjs.com/#example-text