[代码] 点击按钮复制div或者p标签内容

[复制链接]
本站网友  发表于 2022-10-31 22:34 |阅读模式

复制内容到剪切板

  1. <style type="text/css">
  2.    .wrapper {position: relative;}
  3.    #input {position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;}
  4. </style>

  5. <div class="wrapper">
  6.    <p id="text">内容</p>
  7.    <textarea id="input"></textarea>
  8.    <button onclick="copyText()">复制内容</button>
  9. </div>
  10. <script type="text/javascript">
  11.     function copyText() {
  12.       var text = document.getElementById("text").innerText;
  13.       var input = document.getElementById("input");
  14.       input.value = text; // 修改文本框的内容
  15.       input.select(); // 选中文本
  16.       document.execCommand("copy"); // 执行浏览器复制命令
  17.       alert("复制成功");
  18.     }
  19.   </script>
复制代码

QQ|删帖注销|手机版|资源圈

GMT+8, 2024-4-26 18:57

Powered by Discuz!

© 20022-2026 Comsenz Inc.

快速回复 返回顶部 返回列表