javascript - 使用 HTML 按钮将文本复制到剪贴板
问题描述
如何使用 javascript 将一些固定文本复制到剪贴板?
这是我的代码,它不起作用也许还有另一种方法,我不知道。
<button onclick="Copy()">Copy</button>
<script>
function Copy() {
document.execCommand("copy","Some text here");
}
</script>
我想只使用按钮复制一些固定文本,所以我不必手动选择文本并复制它们。谢谢。
解决方案
您可以尝试这种方法:
<script>
function Copy() {
var copyText = document.createElement("input");
copyText.style="display:none";
copyText.value = "This is a paragraph";
document.body.appendChild(copyText);
copyText.select();
copyText.setSelectionRange(0, 99999); /*For mobile devices*/
document.execCommand("copy");
alert("Copied the text: " + copyText.value);
}
</script>
推荐阅读
- gnuplot - 如何在gnuplot 4.0中制作圆圈
- r - 基于三个规则合并数据
- report - URL 链接合并字段的 Apex Salesforce 页面参考
- javascript - 为什么 insertmany 不能在交易中使用 mongoos?
- python - Argparse:如何为一个子命令声明多组参数互斥?如何定义几种使用子命令的方式?
- python - 为熊猫中的所有列生成列矩阵
- sql-server - Crystal Report 仅显示 DetailReport 部分 VB.NET 和 SQL Server 中的第一条记录
- javascript - 禁用某些选择复选框上的按钮
- php - Gcloud 在 LAMP 堆栈上公开 yii
- gremlin - 如何在 gremlinpython 中使用 geoshape