javascript - 从行数据复制到 JavaScript 中的剪贴板
问题描述
function copyPaste(number) {
/* Get the text field */
var copyText = number;
/* Select the text field */
copyText.select();
copyText.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
document.execCommand("copy");
/* Alert the copied text */
alert("Copied the text: " + copyText);
}
<th><p id="phone" onclick="copyPaste(<?php echo $row['phone'];?>)"><?php echo $row['phone']; ?></p></th>
我在没有<p>
标签的情况下工作。它也不起作用..错误:Uncaught TypeError: copyText.select is not a function at copyPaste ((index):109) at HTMLParagraphElement.onclick
来自控制台。我正在关注课程
解决方案
select()
是一种方法HTMLInputElement
。目前,copyText
是一个字符串。要使用copy
命令,您需要在 Html 中创建虚拟输入。
function copyPaste(number) {
var dummy = document.createElement("input");
// Add it to the document
document.body.appendChild(dummy);
// Set value of input
dummy.value = number;
/* Select the text field */
dummy.select();
dummy.setSelectionRange(0, 99999); /* For mobile devices */
/* Copy the text inside the text field */
document.execCommand("copy");
// Remove it as its not needed anymore
document.body.removeChild(dummy);
/* Alert the copied text */
alert("Copied the text: " + number);
}
推荐阅读
- java - 使用相同的正则表达式时,Python 和 Java 正则表达式的行为不同
- neo4j - 如何计算 Neo4j 中浮点值的排名?
- javascript - 使用 Promise 中的值的异步函数
- matlab - 如何 cd 名称中包含日期的文件
- webpack - 为什么在 Node 中使用 UMD 时需要设置 global.window?
- css - 如何使用 Bootstrap Media 对象在第二行制作省略号媒体标题
- html - 为什么我的 html 表单的密码输入字段有浅蓝色背景?
- ruby-on-rails - 来自 Rails API 调用的奇怪响应
- c# - 开发 azure 函数时有没有办法监视文件更改?
- algorithm - 回文链表问题 - 算法