<div style="text-align: center;font-size: 0.213333rem;" class="btn" link-url="http://www.baidu.com">点我复制分享链接</div> <script type="text/javascript"> window.url="http://www.baidu.com" var clipboard = new ClipboardJS('.btn', { text: function() { return window.url; } }); clipboard.on('success', function(e) { console.log(e); }); clipboard.on('error', function(e) { console.log(e); }); </script>
HTML: <input type="hidden" id="content" value="复制的内容"/> <input type="button" id="copyUrl" value="请点击我复制"/> JS: var clipboard2 = new ClipboardJS('#copyUrl', { text: function() { return $("#content").val(); } }); clipboard2.on('success', function(e) { console.log("已复制到剪贴板!"); }); clipboard2.on('error', function (e) { });