复制文本内容到剪贴板

5次阅读

共计 432 个字符,预计需要花费 2 分钟才能阅读完成。

input 输入框中的内容可以复制
通过创建 input 间接实现复制 div 中的文本

<body>
    <div id="box">123456789</div>
    <input type="button" onClick="copyText()" value="点击复制" />
</body>
<script type="text/javascript">
function copyText(){var sText = document.getElementById("box").innerText;
    var oInput = document.createElement('input');
    oInput.value = sText;
    document.body.appendChild(oInput);
    oInput.select(); // 选择对象
    document.execCommand("copy"); // 执行浏览器复制命令
    document.body.removeChild(oInput);
}
</script>
正文完
 0