html 一键复制功能

1.需要引入库:clipboard.min.js
2.代码demo:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script src="./clipboard.min.js"></script>
  <body>
    <span id="ttt">1234567654321</span>
    <button type="button" id="btn_Share" onclick="test('#btn_Share')">
      copy
    </button>
  </body>
  <script>
    function test(element) {
     
     
      var clipboard = new ClipboardJS(element, {
     
           //绑定元素id
        text: function () {
     
     
          var str = document.getElementById("ttt").innerText;
          return str;
        },
      });
      clipboard.on("success", function (e) {
     
     
        alert("复制成功");
      });
      clipboard.on("error", function (e) {
     
     
        alert(e);
      });
    }
  </script>
</html>

加二维码canvas:

https://gera2ld.github.io/qrcanvas/examples/#pure-color

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/112248248