clipboard.js复制

第一种方式

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>clipboard复制文本</title>
</head>
<body>

<p class="newTitle">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p>
<button class="btn" data-clipboard-text="">Copy</button>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/clipboard.js/2.0.4/clipboard.js"></script>

<script>
  $('.btn').attr('data-clipboard-text',$('.newTitle').text()+'\n'+window.location.href+'\n(出处:XXXXXXXXXX)')
  var clipboard = new ClipboardJS('.btn');
  clipboard.on('success', function(e) {
    alert('复制成功')
  });
  clipboard.on('error', function(e) {
    console.log(e);
  });
</script>
</body>
</html>
posted @ 2019-09-16 16:46  (⊙o⊙)买噶  阅读(390)  评论(0编辑  收藏  举报