Clipboard.js不适用于微小的测试

时间:2018-10-23 14:31:43

标签: clipboard.js

我遇到了一个小问题,我尝试使用剪贴板.js,但即使在测试中也无法使用。我的html如下,请告诉我我做错了什么。

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <meta name="description" content="test" />

  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script>
</head>

<body content="text/html">
<button class="btn" data-clipboard-demo="" data-clipboard-action="copy" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">Copy to clipboard</button>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您需要在元素上初始化剪贴板js。将此添加到您的页面:

<script>new ClipboardJS('.btn')</script>

<!DOCTYPE html>
<html>
<head>
  <title>test</title>
  <meta name="description" content="test" />

  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js"></script>
</head>

<body content="text/html">
<button class="btn" data-clipboard-demo="" data-clipboard-action="copy" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">Copy to clipboard</button>
</body>
<script>new ClipboardJS('.btn');</script>
</html>