使用clipboard.js实现复制功能的示例代码(2)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>function-target</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <button class="btn">Copy</button>
  <div>hello</div>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn', {
    target: function() {
      return document.querySelector('div');
    }
  });

  clipboard.on('success', function(e) {
    //console.log(e);
    alert('复制成功!')
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

function-text

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>function-text</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <button class="btn">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn', {
    text: function() {
      return 'to be or not to be';
    }
  });

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

target-div

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>target-div</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <span class="copy_content">hello 123</span>
  <button class="btn" data-clipboard-action="copy" data-clipboard-target=".copy_content">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn');

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>

target-input

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>target-input</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <!-- 1. Define some markup -->
  <input id="foo" type="text" value="hello">
  <button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>

  <!-- 2. Include library -->
  <script src="../dist/clipboard.min.js"></script>

  <!-- 3. Instantiate clipboard -->
  <script>
  var clipboard = new ClipboardJS('.btn');

  clipboard.on('success', function(e) {
    console.log(e);
  });

  clipboard.on('error', function(e) {
    console.log(e);
  });
  </script>
</body>
</html>
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/1257.html