复制-原生js

时间:2021-06-04 19:39:33   收藏:0   阅读:0

复制-原生js

一、原理分析

document.execCommand("copy")

二、代码实现

<div class="wrapper">
    <p id="text">这个个要复制的内容</p>
    <textarea id="input">select()只对input textarea有效</textarea>
    <button onclick="copyText()">copy</button>
</div>
function copyText() {
    var text = document.getElementById("text").innerText;
    var input = document.getElementById("input");
    input.value = text; // 修改文本框的内容
    input.select(); // 选中文本
    document.execCommand("copy"); // 执行浏览器复制命令
    alert("复制成功");
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!