一、原理分析
浏览器提供了 copy 命令 ,可以复制选中的内容
document.execCommand("copy")
二、代码实现
HTML 部分
<div > <textarea id="input">你要复制我?</textarea> <button onclick="copyText()">copy</button> </div>
JS 部分
<script type="text/javascript"> function copyText() { var input = document.getElementById("input"); input.select(); // 选中文本 document.execCommand("copy"); // 执行浏览器复制命令 alert("复制成功"); } </script>
还没有评论,来说两句吧...